Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of matplotlib.colors.Colormap objects as cmap argument results in TypeError: argument of type 'LinearSegmentedColormap' is not iterable #1461

Closed
1 task
vadmbertr opened this issue Dec 2, 2024 · 1 comment · Fixed by #1462

Comments

@vadmbertr
Copy link

vadmbertr commented Dec 2, 2024

Hi,

ALL software version info

(this library, plus any other relevant software, e.g. bokeh, python, notebook, OS, browser, etc should be added within the dropdown below.)

Software Version Info
- macOS 14.6.1
- Python 3.11.10
- holoviews 1.20.0
- matplotlib 3.9.2
- bokeh 3.5.2
- jupyter_bokeh 4.0.5
- cmocean 4.0.3
- jupyterlab 4.3.0

Description of expected behavior and the observed behavior

I'm plotting a 2d field using hvplot.image (same with hvplot.quadmesh) using a colormap from cmocean (same with any matplotlib.colors.Colormap object, including matplotlib.colormaps ones).
I expect it to just do the plot using the provided colormap, but instead I get the following error: TypeError: argument of type 'LinearSegmentedColormap' is not iterable.
I believe it is because the argument passed through cmap is at some point assumed to be a string (not the case here). Adding the dummy argument color="black" and it works as expected (because a test evaluates to False in the implementation).

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

# using cmocean
import cmocean.cm as cmo
import hvplot.xarray  # noqa
import xarray as xr


ds = xr.tutorial.open_dataset("air_temperature")
da = ds.air.isel(time=0)

plot = da.hvplot.image(cmap=cmo.thermal)  # this works: plot = da.hvplot.image(cmap=cmo.thermal, color="black")
# using matplotlib
import hvplot.xarray  # noqa
from matplotlib import colormaps
import xarray as xr


ds = xr.tutorial.open_dataset("air_temperature")
da = ds.air.isel(time=0)

plot = da.hvplot.image(cmap=colormaps["viridis"])  # this works: plot = da.hvplot.image(cmap=colormaps["viridis"] color="black")

Stack traceback and/or browser JavaScript console output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], [line 9](vscode-notebook-cell:?execution_count=5&line=9)
      [6](vscode-notebook-cell:?execution_count=5&line=6) ds = xr.tutorial.open_dataset("air_temperature")
      [7](vscode-notebook-cell:?execution_count=5&line=7) da = ds.air.isel(time=0)
----> [9](vscode-notebook-cell:?execution_count=5&line=9) plot = da.hvplot.image(cmap=cmo.thermal)

File ~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:2155, in hvPlot.image(self, x, y, z, colorbar, **kwds)
   [2103](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:2103) def image(self, x=None, y=None, z=None, colorbar=True, **kwds):
   [2104](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:2104)     """
   [2105](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:2105)     Image plot
   [2106](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:2106) 
   (...)
   [2153](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:2153)     - Plotly: https://plotly.com/python/imshow/
   [2154](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:2154)     """
-> [2155](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:2155)     return self(x, y, z=z, kind='image', colorbar=colorbar, **kwds)

File ~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:95, in hvPlotBase.__call__(self, x, y, kind, **kwds)
     [92](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:92)         plot = self._get_converter(x, y, kind, **kwds)(kind, x, y)
     [93](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:93)         return pn.panel(plot, **panel_dict)
---> [95](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:95) return self._get_converter(x, y, kind, **kwds)(kind, x, y)

File ~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:102, in hvPlotBase._get_converter(self, x, y, kind, **kwds)
    [100](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:100) y = y or params.pop('y', None)
    [101](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:101) kind = kind or params.pop('kind', None)
--> [102](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/plotting/core.py:102) return HoloViewsConverter(self._data, x, y, kind=kind, **params)

File ~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:796, in HoloViewsConverter.__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, robust, symmetric, logx, logy, loglog, hover, hover_cols, hover_formatters, hover_tooltips, subplots, label, invert, stacked, colorbar, cticks, datashade, rasterize, downsample, resample_when, row, col, debug, framewise, aggregator, projection, global_extent, geo, precompute, flip_xaxis, flip_yaxis, dynspread, x_sampling, y_sampling, pixel_ratio, project, tools, attr_labels, coastline, tiles, tiles_opts, sort_date, check_symmetric_max, transforms, stream, cnorm, features, rescale_discrete_levels, autorange, subcoordinate_y, **kwds)
    [793](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:793)     if plotwd in kwds:
    [794](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:794)         plot_opts[plotwd] = kwds.pop(plotwd)
--> [796](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:796) self._style_opts, plot_opts, kwds = self._process_style(kwds, plot_opts)
    [798](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:798) for axis_name in ['xaxis', 'yaxis']:
    [799](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:799)     if axis_name in kwds:

File ~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1488, in HoloViewsConverter._process_style(self, kwds, plot_opts)
   [1486](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1486)     color = style_opts['color']
   [1487](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1487) elif not isinstance(cmap, dict):
-> [1488](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1488)     if cmap and any(c in cmap for c in categories):
   [1489](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1489)         color = process_cmap(cmap or self._default_cmaps['categorical'], categorical=True)
   [1490](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1490)     else:

File ~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1488, in <genexpr>(.0)
   [1486](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1486)     color = style_opts['color']
   [1487](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1487) elif not isinstance(cmap, dict):
-> [1488](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1488)     if cmap and any(c in cmap for c in categories):
   [1489](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1489)         color = process_cmap(cmap or self._default_cmaps['categorical'], categorical=True)
   [1490](https://file+.vscode-resource.vscode-cdn.net/Users/bertrava/projects/vb-phd/notebooks/daily/~/miniconda3/envs/vb-phd/lib/python3.11/site-packages/hvplot/converter.py:1490)     else:

TypeError: argument of type 'LinearSegmentedColormap' is not iterable
  • I may be interested in making a pull request to address this
@vadmbertr vadmbertr changed the title Use of colormap objects result in TypeError: argument of type 'LinearSegmentedColormap' is not iterable Use of matplotlib.colors.Colormap as cmap arguments objects result in TypeError: argument of type 'LinearSegmentedColormap' is not iterable Dec 3, 2024
@vadmbertr vadmbertr changed the title Use of matplotlib.colors.Colormap as cmap arguments objects result in TypeError: argument of type 'LinearSegmentedColormap' is not iterable Use of matplotlib.colors.Colormap objects as cmap argument results in TypeError: argument of type 'LinearSegmentedColormap' is not iterable Dec 3, 2024
@maximlt
Copy link
Member

maximlt commented Dec 11, 2024

Duplicate of #576

@maximlt maximlt marked this as a duplicate of #576 Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants