Bokeh equivalent to matplotlib.imshow
See original GitHub issueIt would be nice to be able to have an equivalent to matplotlib’s imshow (http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow) without having to write a an RGBAColormapper every time.
For example, to get a plot like @mrocklin blogpost images here: http://matthewrocklin.com/blog/work/2015/02/13/Towards-OOC-Slicing-and-Stacking/
in matplotlib you just write:
>>> from matplotlib import imshow
>>> imshow(x[1000] - x.mean(axis=0), cmap='RdBu_r')')
In Bokeh you currently need to write your own colormapper and use image_rgba().
We could add that functionality into image_rgba(). If you don’t pass a palette, then use your input as the RGBA, and if you do pass a bokeh palette name, then you can pass your raw data and the RGBAColorMapper would then return the appropriate rgba values.
Maybe that would look something like:
from bokeh.plotting import image_rgba
p.figure()
p.image_rgba(data, palette='RdBl11')
p.image_rgba(data_rgba) #When already passing the transformation
When working on the ocean slider demo 2.0 https://github.com/ContinuumIO/dask/issues/277 @jcrist already wrote a RGBAColorMapper to be able to draw similar plots in Bokeh:
class RGBAColorMapper(object):
"""Maps floating point values to rgb values over a palette"""
def __init__(self, low, high, palette):
self.range = np.linspace(low, high, len(palette))
self.r, self.g, self.b = np.array(zip(*[hex_to_rgb(i) for i in palette]))
def color(self, data):
"""Maps your data values to the pallette with linear interpolation"""
red = np.interp(data, self.range, self.r)
blue = np.interp(data, self.range, self.b)
green = np.interp(data, self.range, self.g)
# Style plot to return a grey color when value is 'nan'
red[np.isnan(red)] = 240
blue[np.isnan(blue)] = 240
green[np.isnan(green)] = 240
colors = np.dstack([red.astype(np.uint8),
green.astype(np.uint8),
blue.astype(np.uint8),
np.full_like(data, 255, dtype=np.uint8)])
return colors.view(dtype=np.uint32).reshape(data.shape)
colormap = RGBAColorMapper(-6, 6, RdYlBu11)
colormap.color(data)
I think it would be great if we could add that functionality directly into Bokeh. I was actually asked about better image support at Pydata Berlin and Spark Summit.
Thoughts @bokeh/dev?
Issue Analytics
- State:
- Created 8 years ago
- Comments:15 (9 by maintainers)
@bryevdv “How is this substantially different from Figure.image?”
The substantial difference is that in matplotlib I can select from dozens of well-known and well-investigated color maps and that matplotlib generates color map legends so that I’m ready to publish my results. Afaik bokeh still lacks these two features which are already nicely shown by @chdoig. Maybe this is just another issue.
My team would really like to replace the matplotlib plots in our app by bokeh one, as bokeh often looks better, has a much cleaner API, and is also much faster in general. But the missing image features make this a little difficult still.
I feel like I must be missing something. How is this substantially different from
Figure.image
? It accepts a 2d scalar array, and a palette, and colormaps according to the palette on the client:http://bokeh.pydata.org/en/latest/docs/gallery/image.html
This has been part of Bokeh for a very long time.