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

TypeError when rendering a Holomap with Matplotlib whose data is a Dask DataFrame #5111

Open
maximlt opened this issue Nov 5, 2021 · 0 comments
Labels
tag: backend: mpl tag: hvplot Issue originally related to hvplot type: bug Something isn't correct or isn't working

Comments

@maximlt
Copy link
Member

maximlt commented Nov 5, 2021

Rendering an histogram whose source is a grouped-by dask array raises an error (TypeError: len() of unsized object) with the Matplotlib backend (works fine with Bokeh and Plotly).

import holoviews as hv
from hvplot.sample_data import airline_flights
from holoviews.operation import histogram

hv.extension('matplotlib')

flights = airline_flights.to_dask().persist()
flight_subset = flights[flights.carrier.isin(['AA', 'US', 'OH'])]
by = 'carrier'

ds = hv.Dataset(flight_subset, by)
print(ds)

ds_grouped = ds.groupby(by)
print(ds_grouped)

hists = histogram(ds_grouped, dimension='depdelay')
print(hists)

hists
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/holoviews/core/dimension.py in _repr_mimebundle_(self, include, exclude)
   1314         combined and returned.
   1315         """
-> 1316         return Store.render(self)
   1317 
   1318 

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/holoviews/core/options.py in render(cls, obj)
   1403         data, metadata = {}, {}
   1404         for hook in hooks:
-> 1405             ret = hook(obj)
   1406             if ret is None:
   1407                 continue

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/holoviews/ipython/display_hooks.py in pprint_display(obj)
    280     if not ip.display_formatter.formatters['text/plain'].pprint:
    281         return None
--> 282     return display(obj, raw_output=True)
    283 
    284 

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/holoviews/ipython/display_hooks.py in display(obj, raw_output, **kwargs)
    256     elif isinstance(obj, (HoloMap, DynamicMap)):
    257         with option_state(obj):
--> 258             output = map_display(obj)
    259     elif isinstance(obj, Plot):
    260         output = render(obj)

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/holoviews/ipython/display_hooks.py in wrapped(element)
    144         try:
    145             max_frames = OutputSettings.options['max_frames']
--> 146             mimebundle = fn(element, max_frames=max_frames)
    147             if mimebundle is None:
    148                 return {}, {}

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/holoviews/ipython/display_hooks.py in map_display(vmap, max_frames)
    204         return None
    205 
--> 206     return render(vmap)
    207 
    208 

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/holoviews/ipython/display_hooks.py in render(obj, **kwargs)
     66         renderer = renderer.instance(fig='png')
     67 
---> 68     return renderer.components(obj, **kwargs)
     69 
     70 

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/holoviews/plotting/renderer.py in components(self, obj, fmt, comm, **kwargs)
    408                 doc = Document()
    409                 with config.set(embed=embed):
--> 410                     model = plot.layout._render_model(doc, comm)
    411                 if embed:
    412                     return render_model(model, comm)

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/panel/viewable.py in _render_model(self, doc, comm)
    460         if comm is None:
    461             comm = state._comm_manager.get_server_comm()
--> 462         model = self.get_root(doc, comm)
    463 
    464         if config.embed:

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/panel/viewable.py in get_root(self, doc, comm, preprocess)
    518         """
    519         doc = init_doc(doc)
--> 520         root = self._get_model(doc, comm=comm)
    521         if preprocess:
    522             self._preprocess(root)

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/panel/layout/base.py in _get_model(self, doc, root, parent, comm)
    120         if root is None:
    121             root = model
--> 122         objects = self._get_objects(model, [], doc, root, comm)
    123         props = dict(self._init_params(), objects=objects)
    124         model.update(**self._process_param_change(props))

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/panel/layout/base.py in _get_objects(self, model, old_objects, doc, root, comm)
    110             else:
    111                 try:
--> 112                     child = pane._get_model(doc, root, model, comm)
    113                 except RerenderError:
    114                     return self._get_objects(model, current_objects[:i], doc, root, comm)

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/panel/pane/holoviews.py in _get_model(self, doc, root, parent, comm)
    262         child_pane = self._panes.get(backend, Pane)(state, **kwargs)
    263         self._update_plot(plot, child_pane)
--> 264         model = child_pane._get_model(doc, root, parent, comm)
    265         if ref in self._plots:
    266             old_plot, old_pane = self._plots[ref]

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/panel/pane/plot.py in _get_model(self, doc, root, parent, comm)
    191     def _get_model(self, doc, root=None, parent=None, comm=None):
    192         if not self.interactive:
--> 193             return PNG._get_model(self, doc, root, parent, comm)
    194         self.object.set_dpi(self.dpi)
    195         manager = self._get_widget(self.object)

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/panel/pane/markup.py in _get_model(self, doc, root, parent, comm)
     39 
     40     def _get_model(self, doc, root=None, parent=None, comm=None):
---> 41         model = self._bokeh_model(**self._get_properties())
     42         if root is None:
     43             root = model

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/panel/pane/image.py in _get_properties(self)
    107         if self.object is None:
    108             return dict(p, text='<img></img>')
--> 109         data = self._data()
    110         if not isinstance(data, bytes):
    111             data = base64.b64decode(data)

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/panel/pane/plot.py in _data(self)
    234             bbox_inches = None
    235 
--> 236         self.object.canvas.print_figure(b, bbox_inches=bbox_inches)
    237         return b.getvalue()
    238 

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
   2259                         orientation=orientation,
   2260                         bbox_inches_restore=_bbox_inches_restore,
-> 2261                         **kwargs)
   2262             finally:
   2263                 if bbox_inches and restore_bbox:

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/backend_bases.py in wrapper(*args, **kwargs)
   1667             kwargs.pop(arg)
   1668 
-> 1669         return func(*args, **kwargs)
   1670 
   1671     return wrapper

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, *args)
    506             *metadata*, including the default 'Software' key.
    507         """
--> 508         FigureCanvasAgg.draw(self)
    509         mpl.image.imsave(
    510             filename_or_obj, self.buffer_rgba(), format="png", origin="upper",

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in draw(self)
    404              (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
    405               else nullcontext()):
--> 406             self.figure.draw(self.renderer)
    407             # A GUI class may be need to update a window using this draw, so
    408             # don't forget to call the superclass.

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     72     @wraps(draw)
     73     def draw_wrapper(artist, renderer, *args, **kwargs):
---> 74         result = draw(artist, renderer, *args, **kwargs)
     75         if renderer._rasterizing:
     76             renderer.stop_rasterizing()

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     49                 renderer.start_filter()
     50 
---> 51             return draw(artist, renderer, *args, **kwargs)
     52         finally:
     53             if artist.get_agg_filter() is not None:

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/figure.py in draw(self, renderer)
   2779             self.patch.draw(renderer)
   2780             mimage._draw_list_compositing_images(
-> 2781                 renderer, self, artists, self.suppressComposite)
   2782 
   2783             for sfig in self.subfigs:

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    130     if not_composite or not has_images:
    131         for a in artists:
--> 132             a.draw(renderer)
    133     else:
    134         # Composite any adjacent images together

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     49                 renderer.start_filter()
     50 
---> 51             return draw(artist, renderer, *args, **kwargs)
     52         finally:
     53             if artist.get_agg_filter() is not None:

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/_api/deprecation.py in wrapper(*inner_args, **inner_kwargs)
    429                          else deprecation_addendum,
    430                 **kwargs)
--> 431         return func(*inner_args, **inner_kwargs)
    432 
    433     return wrapper

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
   2919             renderer.stop_rasterizing()
   2920 
-> 2921         mimage._draw_list_compositing_images(renderer, self, artists)
   2922 
   2923         renderer.close_group('axes')

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    130     if not_composite or not has_images:
    131         for a in artists:
--> 132             a.draw(renderer)
    133     else:
    134         # Composite any adjacent images together

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     49                 renderer.start_filter()
     50 
---> 51             return draw(artist, renderer, *args, **kwargs)
     52         finally:
     53             if artist.get_agg_filter() is not None:

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/patches.py in draw(self, renderer)
    605                 self._bind_draw_path_function(renderer) as draw_path:
    606             path = self.get_path()
--> 607             transform = self.get_transform()
    608             tpath = transform.transform_path_non_affine(path)
    609             affine = transform.get_affine()

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/patches.py in get_transform(self)
    271     def get_transform(self):
    272         """Return the `~.transforms.Transform` applied to the `Patch`."""
--> 273         return self.get_patch_transform() + artist.Artist.get_transform(self)
    274 
    275     def get_data_transform(self):

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/patches.py in get_patch_transform(self)
    775         # important to call the accessor method and not directly access the
    776         # transformation member variable.
--> 777         bbox = self.get_bbox()
    778         return (transforms.BboxTransformTo(bbox)
    779                 + transforms.Affine2D().rotate_deg_around(

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/patches.py in get_bbox(self)
    854     def get_bbox(self):
    855         """Return the `.Bbox`."""
--> 856         x0, y0, x1, y1 = self._convert_units()
    857         return transforms.Bbox.from_extents(x0, y0, x1, y1)
    858 

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/patches.py in _convert_units(self)
    764     def _convert_units(self):
    765         """Convert bounds of the rectangle."""
--> 766         x0 = self.convert_xunits(self._x0)
    767         y0 = self.convert_yunits(self._y0)
    768         x1 = self.convert_xunits(self._x0 + self._width)

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/artist.py in convert_xunits(self, x)
    201         if ax is None or ax.xaxis is None:
    202             return x
--> 203         return ax.xaxis.convert_units(x)
    204 
    205     def convert_yunits(self, y):

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/axis.py in convert_units(self, x)
   1489     def convert_units(self, x):
   1490         # If x is natively supported by Matplotlib, doesn't need converting
-> 1491         if munits._is_natively_supported(x):
   1492             return x
   1493 

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/matplotlib/units.py in _is_natively_supported(x)
     64     if np.iterable(x):
     65         # Assume lists are homogeneous as other functions in unit system.
---> 66         for thisx in x:
     67             if thisx is ma.masked:
     68                 continue

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/dask/array/core.py in __iter__(self)
   1480 
   1481     def __iter__(self):
-> 1482         for i in range(len(self)):
   1483             yield self[i]
   1484 

~/miniconda3/envs/hvplot-dev37/lib/python3.7/site-packages/dask/array/core.py in __len__(self)
   1354     def __len__(self):
   1355         if not self.chunks:
-> 1356             raise TypeError("len() of unsized object")
   1357         return sum(self.chunks[0])
   1358 

TypeError: len() of unsized object
@maximlt maximlt added the type: bug Something isn't correct or isn't working label Nov 5, 2021
@maximlt maximlt added the tag: hvplot Issue originally related to hvplot label Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tag: backend: mpl tag: hvplot Issue originally related to hvplot type: bug Something isn't correct or isn't working
Projects
None yet
Development

No branches or pull requests

1 participant