holoviz-topics / EarthSim

Tools for working with and visualizing environmental simulations.
https://earthsim.holoviz.org
BSD 3-Clause "New" or "Revised" License
65 stars 21 forks source link

Custom colorbar generation #112

Closed kcpevey closed 6 years ago

kcpevey commented 6 years ago

I tried a while back to create a custom colormap for use in a holoviews/geoviews drawing and I could never get it to work. I have ranges and colors:

levels = ((155,999),(131-155),(111-130),(96-110),(74-95),(35-73),(0,35))  
colors = ['#ff6060', '#ff8f20', '#ffc140', '#ffe775', '#ffffcc', '#00faf4', '#5ebaff']  

I got confused on where the colormap generation is sitting. Is it in bokeh? Or is it in another package?

And then I suppose I'll implement it by setting it here in place of RdYlBu
%%opts Path (cmap='RdYlBu') [color_index='Max_Wind_Speed', colorbar=True]

Can I get some direction?

jbednar commented 6 years ago

With HoloViews and GeoViews you can use either Matplotlib colormaps or Bokeh palettes with Bokeh; either one should be fine. Here, colors is a valid Bokeh palette, so you can pass cmap=colors instead of cmap='RdYlBu', which should work fine, but it won't respect your levels. You can write some Python to duplicate the given colors to cover the given levels (copy the last color 35 times, etc.), and the resulting list will be a valid Bokeh palette as well. But presumably there is some code already, maybe in Matplotlib, that will take separate levels and colors lists and turn it into a Matplotlib colormap? If so just use that and pass in the resulting object for the cmap.

kcpevey commented 6 years ago

So bokeh would just take colors and divide it evenly over the range of the plotted data? Would that follow the original data or redim.range?

presumably there is some code already, maybe in Matplotlib, that will take separate levels and colors lists and turn it into a Matplotlib colormap?

This is what I'm looking for. I'd prefer a utility that will fill out a continuous color spread for me. I'll see what matplotlib has available.

kcpevey commented 6 years ago

Actually for the levels example above, I need a discrete colorbar, but for others I need continuous. At any rate, a quick google search pointed me to some simple matplotlib applications for both of those.
https://matplotlib.org/tutorials/colors/colorbar_only.html
https://matplotlib.org/2.0.1/examples/pylab_examples/custom_cmap.html

Can I use bokeh as my backend and use a matplotlib colorbar?

jbednar commented 6 years ago

So bokeh would just take colors and divide it evenly over the range of the plotted data? Would that follow the original data or redim.range?

Bokeh takes that list and divides it evenly over the range 0,255. Separately, whatever range is being plotted by HoloViews is mapped into the range 0, 255. By default, this range will be the data range, but it will be whatever range is in use at the time if you've told it something else.

I'd prefer a utility that will fill out a continuous color spread for me. I'll see what matplotlib has available.

The LinearSegmentedColormap from Matplotlib will interpolate between colors to give a continuous set of colors. I don't know of one to do discrete colors covering certain ranges, but there probably is one.

Can I use bokeh as my backend and use a matplotlib colorbar?

In HoloViews, yes, as long as you have both Bokeh and Matplotlib installed. And as long as you mean "colormap", i.e., the mapping from values to colors, and not "colorbar", i.e. the graphical display of that mapping. You'll need to use Bokeh's colorbars for a Bokeh plot, but HoloViews will convert a Matplotlib colormap into a Bokeh palette as needed (for convenience), so you can use a Matplotlib colormap wherever you like.

jbednar commented 6 years ago

I haven't used it, but from the description, Matplotlib's from_levels_and_colors might be what you need for the discrete case.

philippjfr commented 6 years ago

Note that the from_levels_and_colors returns both a colormap and a Normalize instance, I suspect you'd have to manually set the ranges specified by the normalize instance. We might want to add an example like that to the colormap or styling guides in holoviews.

jbednar commented 6 years ago

Right; without trying it out I can't see whether the colormap alone that gets constructed is useful for this by itself, or whether it only works in conjunction with the separate normalizer. Definitely something that is worth covering in the hv userguide; there was just an SO issue about this today.

kcpevey commented 6 years ago

I'm planning to demo my workflow notebooks on our call tomorrow (and I can send a copy if need be). I can show you what I have so far (which is not currently working like I want) and you guys can see what it is I'm trying to achieve.

kcpevey commented 6 years ago

Can I get a link to the userguide when that becomes available?

philippjfr commented 6 years ago

It will be in the Custom color intervals section of this user guide after the next release.