I am making a bubble chart using Bokeh and want the ColorBar to show the min and max value. Given data that looks like this
In[23]: group_counts.head()
cyl yr counts size
0 3 72 1 0.854701
1 3 73 1 0.854701
2 3 77 1 0.854701
3 3 80 1 0.854701
4 4 70 7 5.982906
I am generating a plot using
x_col = 'cyl'
y_col = 'yr'
color_transformer = transform.linear_cmap('counts', Inferno256,
group_counts.counts.min(),
group_counts.counts.max())
color_bar = ColorBar(color_mapper=color_transformer['transform'],
location=(0, 0))
source = ColumnDataSource(data=group_counts)
p = plotting.figure(x_range=np.sort(group_counts[x_col].unique()),
y_range=np.sort(group_counts[y_col].unique()),
plot_width=400, plot_height=300,
x_axis_label=x_col, y_axis_label=y_col)
p.add_layout(color_bar, 'right')
p.scatter(x=x_col, y=y_col, size='size', color=color_transformer,
source=source)
plotting.show(p)
Notice that the min and max values on the colorbar are not labelled. How do I force the colorbar to label these values?
You can do this using the FixedTicker
class, located under bokeh.models
. It is meant to be used to,
Generate ticks at fixed, explicitly supplied locations.
To provide the min and max data values, specify the desired tick values then pass that object to ColorBar
using the ticker
keyword argument.
mn = group_counts.counts.min()
mx = group_counts.counts.max()
n_ticks = 5 # how many ticks do you want?
ticks = np.linspace(mn, mx, n_ticks).round(1) # round to desired precision
color_ticks = FixedTicker(ticks=ticks)
color_bar = ColorBar(color_mapper=color_transformer['transform'],
location=(0, 0),
ticker=color_ticks, # <<< pass ticker object
)
If you want something a bit more exotic, there are 14 different tickers currently described in the bokeh.models.tickers
documentation (do a word search for Ticker(**
to quickly jump between the different options).