hvplot icon indicating copy to clipboard operation
hvplot copied to clipboard

Matplotlib colormaps not directly supported anymore

Open benbovy opened this issue 4 years ago • 1 comments

ALL software version info

hvplot: 0.7.1 holoviews: 1.14.2 matplotlib: 3.3.4

Description of expected behavior and the observed behavior

Passing matplotlib colormaps to hvplot methods used to be supported, but it doesn't seem to be the case anymore (I suspect since #548), unless explicitly wrapped by holoviews.plotlling.util.process_cmap. Is it a bug or is it intentional?

Complete, minimal, self-contained example code that reproduces the issue

Setup:

import numpy as np
import xarray as xr

import matplotlib.pyplot as plt
import holoviews as hv
import hvplot.xarray

da = xr.DataArray(np.random.rand(100, 100))

Using directly a matplotlib colormap object gives the error below:

>>> da.hvplot.image(cmap=plt.cm.viridis)

This works:

>>> da.hvplot.image(cmap=hv.plotting.util.process_cmap(plt.cm.viridis))

Stack traceback and/or browser JavaScript console output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-60af46d7f70b> in <module>
----> 1 da.hvplot.image(cmap=plt.cm.viridis)

~/miniconda3/envs/fastscape-demo/lib/python3.8/site-packages/hvplot/plotting/core.py in image(self, x, y, z, colorbar, **kwds)
    625             The HoloViews representation of the plot.
    626         """
--> 627         return self(x, y, z=z, kind='image', colorbar=colorbar, **kwds)
    628 
    629     def rgb(self, x=None, y=None, z=None, bands=None, **kwds):

~/miniconda3/envs/fastscape-demo/lib/python3.8/site-packages/hvplot/plotting/core.py in __call__(self, x, y, kind, **kwds)
     77                 return pn.panel(plot, **panel_dict)
     78 
---> 79         return self._get_converter(x, y, kind, **kwds)(kind, x, y)
     80 
     81     def _get_converter(self, x=None, y=None, kind=None, **kwds):

~/miniconda3/envs/fastscape-demo/lib/python3.8/site-packages/hvplot/plotting/core.py in _get_converter(self, x, y, kind, **kwds)
     84         y = y or params.pop('y', None)
     85         kind = kind or params.pop('kind', None)
---> 86         return HoloViewsConverter(
     87             self._data, x, y, kind=kind, **params
     88         )

~/miniconda3/envs/fastscape-demo/lib/python3.8/site-packages/hvplot/converter.py in __init__(self, data, x, y, kind, by, use_index, group_label, value_label, backlog, persist, use_dask, crs, fields, groupby, dynamic, grid, legend, rot, title, xlim, ylim, clim, symmetric, logx, logy, loglog, hover, subplots, label, invert, stacked, colorbar, datashade, rasterize, row, col, figsize, debug, framewise, aggregator, projection, global_extent, geo, precompute, flip_xaxis, flip_yaxis, dynspread, hover_cols, x_sampling, y_sampling, project, tools, attr_labels, coastline, tiles, sort_date, check_symmetric_max, transforms, stream, **kwds)
    433                 plot_opts[plotwd] = kwds.pop(plotwd)
    434 
--> 435         self._style_opts, plot_opts, kwds = self._process_style(kwds, plot_opts)
    436 
    437         for axis_name in ['xaxis', 'yaxis']:

~/miniconda3/envs/fastscape-demo/lib/python3.8/site-packages/hvplot/converter.py in _process_style(self, kwds, plot_opts)
    917             color = style_opts['color']
    918         elif not isinstance(cmap, dict):
--> 919             if cmap and any(c in cmap for c in categories):
    920                 color = process_cmap(cmap or self._default_cmaps['categorical'], categorical=True)
    921             else:

~/miniconda3/envs/fastscape-demo/lib/python3.8/site-packages/hvplot/converter.py in <genexpr>(.0)
    917             color = style_opts['color']
    918         elif not isinstance(cmap, dict):
--> 919             if cmap and any(c in cmap for c in categories):
    920                 color = process_cmap(cmap or self._default_cmaps['categorical'], categorical=True)
    921             else:

TypeError: argument of type 'ListedColormap' is not iterable

benbovy avatar Mar 17 '21 14:03 benbovy

Looks like a bug, let's try to fix asap and get 0.7.2 out.

philippjfr avatar Mar 17 '21 15:03 philippjfr