Does bokeh need a function for diverging asymmetric colorbar

I figured out how create a diverging asymmetric colorbar in bokeh and use it in a bokeh chart. Was so happy about that I wrote a blog post about it!
https://www.rbtechblog.com/blog/stocks2018#colorbar
And @BokehPlots has tweeted about it!

I am wondering if this is something you would like to put in the bokeh library. This is somewhere I could contribute to bokeh with a little guidance. First though, is it needed? I have used diverging asymmetric colorbars just a handful times in last few years as a data scientist that creates many visualizations. Too many high level functions can really clutter up the interface!

1 Like

@russellburdt I think it could make a nice addition, but I would suggest some tweaks:

  • apart from figure, it’s atypical in Bokeh to have functions that return configured Models or assemblages of Models. So there isn’t an existing place this naturally fits in as-is. Instead I’d suggest a function that skips the last step, and returns only the array of colors.

  • the function can also generate symmetric diverging colormaps as a special case, so I’d suggest making the name more general

Taken together, I would propose adding a new function diverging_palette (or maybe generate_diverging_palette if we want to be more verbose). Such a function would make perfect sense to put inside the bokeh.palettes module. What do you think?

@Bryan Thanks for pointing me to the palettes module.

I see the contribution as resembling the linear_palette method, which takes palette and n as arguments. Towards that end a good name for the contribution would be diverging_palette (as you recommended) with arguments palette1, palette2, n, midpoint=0.5, and reverse=True. The function would return a list of hex colors representing the diverging palette (symmetric by default). An asymmetric diverging palette could be created by adjusting the midpoint argument away from its default value.

What do you think about that approach?

@russellburdt That sounds perfect. I’d suggests modeling tests and docs off the ones that exist already for linear_palette. Please let me know if you have any questions about those!

@Bryan Could you point me to information on tests and docs for this contribution.

Examples of palettes returned by diverging_palette are below. The function builds and returns a diverging palette from arguments palette1 and palette2. Optional arguments support a number of colors n and a relative location where the palette diverges midpoint. Default behavior is to return a 256-color symmetric palette.

@Bryan What do you think about adding additional 256-color palettes to bokeh?
In order to create the picture in my last response I needed to create a bokeh Reds256 palette and the same for Purples and Blues. There was already a Greys256 palette in bokeh 1.3.4.

The additional palettes can be created by hard-coding a list of hex colors created by matplotlib:
palette = [matplotlib.colors.to_hex(x) for x in matplotlib.cm.Reds(np.arange(256) / 255)]
palettes.Reds256 = palette
That is, the list would be hard-coded as palettes.Reds256 and matplotlib would not become a dependency of bokeh.

Here are some random 3D data visualizations create using diverging_palette
Data are exactly the same in each, only the colorbar is different.

@russellburdt I am pretty indifferent about adding either new 256 color palettes, or new palette generating functions. I would suggest raising the issue in a PR so we can try to get other comments from @bokeh/dev.

For docs, I would just say imitate the similar analogous functions already in bokeh.palettes.

It turns out there is not much test coverage for palettes:

https://github.com/bokeh/bokeh/blob/master/bokeh/tests/test_palettes.py

That’s probably because most of the content is just data. But I think though since your function computes new palettes it make sense to add a unit test or two in that test module to make sure that the expected output is generated in some test cases.

@Bryan Has been documented as Issue #9241. Will wait for comments before opening a PR.

1 Like