Using Matplotlib Normalized Colobars

I’d like to use Matplotlib’s colorbar normalization when plotting with hvplot.image(). For instance, I’d like to use the Matplotlib function PowerNorm (matplotlib.colors.PowerNorm — Matplotlib 3.10.0 documentation).

Does anyone know if there is a way to do this and if so, what’s the best way? Can the “norm=” argument be used to pass a colorbar? I’ve so far been unsuccessful at the latter and I’m hoping maybe someone here knows how best to do this. Thanks.

hvplot=0.11.2
holoviews=1.20.0
matplotlib=3.10.0

Aaron

Theoretically cnorm

Alternatively, you could use hooks to manually add the colorbar:
https://holoviews.org/user_guide/Customizing_Plots.html#plot-hooks

Or also backend_opts
https://holoviews.org/user_guide/Plotting_with_Matplotlib.html#setting-backend-opts

import hvplot.xarray
import xarray as xr
import holoviews as hv

hv.extension("matplotlib")

ds = xr.tutorial.open_dataset("air_temperature")
hv.help(hv.Image(ds.isel(time=0), ["lon", "lat"]).opts(colorbar=True))

Thank you for the reply. I’ll try the plot hooks and see if I can get it to work.

I wasn’t able to figure out how to pass in a normalization when creating the plot in hvplot, but I was able to hvplot.render(plot) to get the Matplotlib figure and then snoop around the fig’s axes.collections and use the update_norm() function to achieve what I needed.

I think you can extract your downstream code from render

fig = hvplot.render(plot)
... your code

into hook

def colorbar_hook(plot, element):
    fig = plot.handles["figure"]
   ...your code

ds.hvplot().opts(hooks=[colorbar_hook])

Thanks. I appreciate the suggestion.