Reputation: 707
I've made a heatmap based on the following dataframe :
Species categories value1 value2
0 sp1 cat1 218.0 2.0
1 sp1 cat2 521.0 2.0
2 sp1 cat3 533.0 2.0
3 sp1 cat4 793.0 3.0
4 sp2 cat1 225.0 2.0
5 sp2 cat2 521.0 2.0
6 sp2 cat3 540.0 2.0
7 sp2 cat4 800.0 3.0
8 sp3 cat1 217.0 1.0
9 sp3 cat2 477.0 1.0
10 sp3 cat3 512.0 3.0
11 sp3 cat4 725.0 3.0
The column categories
is the x-axis column Species
is the y-axis. The heatmap elements are circles, with a color range based on the value of value1
and a circle size based on value2
.
My point is about legends ; I managed to draw a color_bar
scale for value1
thanks to bokeh tutorials, but I can't find how to make a legend for value2
...
My code looks like something like this :
# stuff
colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"]
mapper = LinearColorMapper(palette=colors, low=df.value1.min(), high=df.value1.max())
xrange = list(df.columns)
yrange = list(df.index)
# figure
p = figure(title='title',
x_range=xrange, y_range=yrange,
x_axis_location="above", plot_width=400, plot_height=200,
toolbar_location=None, tools=""
)
# draw the circles
p.circle(x="categories", y="Species", size="value2",
source=df,
fill_color={'field': "value1", 'transform': mapper},
line_color=None)
# Legend for circles colors
color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="10pt",
ticker=BasicTicker(desired_num_ticks=len(colors)),
formatter=PrintfTickFormatter(format="%.3f"),
label_standoff=15, border_line_color=None, location=(0, 0))
p.add_layout(color_bar, 'right')
# missing : legend for circles size !
Is there a way to do that kind of legend ?
Upvotes: 0
Views: 826
Reputation: 34618
As of Bokeh 1.4.0
this is still and open issue and there is not a good built in way to do this. I have re-triaged that issue to be in a shorter term milestone, though always with OSS, the only thing that is guaranteed to help tasks and features happen more quickly is for more contributors to pitch in.
It's possible you could create a custom extension to draw a specialized legend:
https://docs.bokeh.org/en/latest/docs/user_guide/extensions.html
That would require more iteration and back-and-forth discussion than is appropriate for an SO answer, but please feel free to come by the project Discourse.
Upvotes: 0