简体   繁体   中英

How do I get a Bokeh ColorBar to show the min and max value?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM