question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bokeh equivalent to matplotlib.imshow

See original GitHub issue

It 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/

image

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:closed
  • Created 8 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
formancommented, Jul 22, 2016

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

2reactions
bryevdvcommented, May 4, 2016

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Equivalent vmin vmax matplotlib bokeh - python - Stack Overflow
In the bokeh library, what is the equivalent of vmax and vmax of matplolib imshow? For example, in Matplolib I use vmin and...
Read more >
image.py — Bokeh 2.4.3 Documentation
A plot that demonstrates rendering images by colormapping scalar arrays. Details. Bokeh APIs. Figure.image. More info. Plotting with basic glyphs > Images.
Read more >
Data Visualization in Python with matplotlib, Seaborn, and ...
In this post, we will use matplotlib, Seaborn, and Bokeh. ... Then we will display each image on each axes object using the...
Read more >
Lesson 42: Interactive plotting with Bokeh and HoloViews
Bokeh (pronounced "BOH-kay") facilitates this. While I like Matplotlib and we've used it to great effect in the bootcamp, I actually prefer Bokeh's...
Read more >
PyPortrait: Simulating Bokeh with Python - | notebook.community
In photography, bokeh is the aesthetic quality of the blur produced in the ... data %matplotlib inline cat = data.chelsea() plt.imshow(cat) plt.axis('off') ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found