Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I plot a colorbar for a bokeh heatmap?

Does bokeh have a simple way to plot the colorbar for a heatmap?

In this example it would be a strip illustrating how colors correspond to values.

In matlab, its called a 'colorbar' and looks like this: enter image description here

like image 496
bs123 Avatar asked Sep 16 '15 17:09

bs123


People also ask

How to create a heatmap in bokeh?

A solution for creating Heatmaps in Bokeh is using p.rect () as instructed in this link about unemployment.py, which results in something like this: Show activity on this post.

Is it possible to create Heatmaps in bkcharts?

Charts was moved to bkcharts package, which was than discontinued (further reference in in this answer ). Holoviews still has some support for Bokeh, but has some different syntax. A solution for creating Heatmaps in Bokeh is using p.rect () as instructed in this link about unemployment.py, which results in something like this:

What is a heatmap?

What is a heatmap? A heatmap (aka heat map) depicts values for a main variable of interest across two axis variables as a grid of colored squares. The axis variables are divided into ranges like a bar chart or histogram, and each cell’s color indicates the value of the main variable in the corresponding cell range.

What are the visualization interfaces that bokeh provides?

Now, there are various visualization interfaces that bokeh provides us such as bokeh.models, bokeh.plotting, bokeh.transform etc so that we can import various modules out of it and use them to create and define various properties in the plots.


2 Answers

UPDATE: This is now much easier: see

http://docs.bokeh.org/en/latest/docs/user_guide/annotations.html#color-bars


I'm afraid I don't have a great answer, this should be easier in Bokeh. But I have done something like this manually before.

Because I often want these off my plot, I make a new plot, and then assemble it together with something like hplot or gridplot.

There is an example of this here: https://github.com/birdsarah/pycon_2015_bokeh_talk/blob/master/washmap/washmap/water_map.py#L179

In your case, the plot should be pretty straight forward. If you made a datasource like this:

| value | color
| 1     | blue
.....
| 9     | red

Then you could do something like:

legend = figure(tools=None)
legend.toolbar_location=None
legend.rect(x=0.5, y='value', fill_color='color', width=1, height=1, source=source)
layout = hplot(main, legend)
show(legend)

However, this does rely on you knowing the colors that your values correspond to. You can pass a palette to your heatmap chart call - as shown here: http://docs.bokeh.org/en/latest/docs/gallery/cat_heatmap_chart.html so then you would be able to use that to construct the new data source from that.

I'm pretty sure there's at least one open issue around color maps. I know I just added one for off-plot legends.

like image 79
birdsarah Avatar answered Sep 17 '22 14:09

birdsarah


Since other answers here seem very complicated, here an easily understandable piece of code that generates a colorbar on a bokeh heatmap.

import numpy as np
from bokeh.plotting import figure, show
from bokeh.models import LinearColorMapper, BasicTicker, ColorBar


data = np.random.rand(10,10)

color_mapper = LinearColorMapper(palette="Viridis256", low=0, high=1)

plot = figure(x_range=(0,1), y_range=(0,1))
plot.image(image=[data], color_mapper=color_mapper,
           dh=[1.0], dw=[1.0], x=[0], y=[0])

color_bar = ColorBar(color_mapper=color_mapper, ticker= BasicTicker(),
                     location=(0,0))

plot.add_layout(color_bar, 'right')

show(plot)

enter image description here

like image 43
ImportanceOfBeingErnest Avatar answered Sep 19 '22 14:09

ImportanceOfBeingErnest