Geralt
Geralt

Reputation: 180

Set colors according to values in a ColumnDataSource in Bokeh's scatter plot

I'm using a pandas dataframe as a ColumnDataSource in Bokeh to plot a scatter where each bubble represents a country. In my dataframe there's a column named "region" informing the region of each country, and I want the bubbles to be in the color that corresponds to their regions. I'd like to use a palette (like Viridis) to do so, but I'm having trouble to understand how that works in Bokeh, since I'm more used to MPL.

source = ColumnDataSource(data=dict(x=df['gdp'], 
                                y=df['lifeExpec'], 
                                s=df['population']/100000))

p = figure(title='Bokeh Bubble Chart',
       width=600,
       height=500,
       x_axis_type='linear',
       y_axis_type='linear',
       x_range=(1000, 90000), 
       y_range=(0, 100))

p.scatter(source=source,
      x='x',
      y='y',
      radius = 's',
      marker="circle",
      alpha = 0.5)

show(p)

Upvotes: 2

Views: 5265

Answers (1)

Geralt
Geralt

Reputation: 180

Got it. Stored all the regions in a variable in my dictionary and then used factor_cmap to set a color for each region.

regions = df['region']

source = ColumnDataSource(data=dict(x=df2['gdp'], 
                                y=df2['lifeExpec'], 
                                s=df2['population']/100000,
                                ur = regions))

colors = factor_cmap('ur', palette=Category20b_20, factors=regions.unique()) 

p.scatter(source=source,
          x='x',
          y='y',
          radius = 's',
          marker="circle",
          alpha = 0.5,
          fill_color=colors,
          line_color=colors)

enter image description here

Upvotes: 3

Related Questions