jwass / mplleaflet

Easily convert matplotlib plots from Python into interactive Leaflet web maps.
BSD 3-Clause "New" or "Revised" License
521 stars 76 forks source link

AttributeError: 'PatchCollection' object has no attribute 'get_offset_position' #83

Open pbartusch opened 2 years ago

pbartusch commented 2 years ago

Hi,

I am currently reproducing code for plotting maps of Germany (https://juanitorduz.github.io/germany_plots/).

I try to get the interactive map running:

import mplleaflet

fig, ax = plt.subplots()

berlin_df.plot(
    ax=ax,
    alpha=0.2
)

berlin_neighbourhoods_df.plot(
    ax=ax,
    column='neighbourhood_group',
    categorical=True,
    cmap='tab20',
)

mplleaflet.display(fig=fig)

(one of the last steps of the tutorial)

I already fixed the _gridOnMajor bug like suggested here: https://github.com/jwass/mplleaflet/issues/80 (thanks to @22tommibaer01).

Unfortunately, I ran into another bug with the following error log:

[...]
File ~/.pyenv/versions/3.10.1/lib/python3.10/site-packages/mplleaflet/mplexporter/exporter.py:263, in Exporter.draw_collection(self, ax, collection, force_pathtrans, force_offsettrans)
    255 styles = {'linewidth': collection.get_linewidths(),
    256           'facecolor': collection.get_facecolors(),
    257           'edgecolor': collection.get_edgecolors(),
    258           'alpha': collection._alpha,
    259           'zorder': collection.get_zorder()}
    261 offset_dict = {"data": "before",
    262                "screen": "after"}
--> 263 offset_order = offset_dict[collection.get_offset_position()]
    265 self.renderer.draw_path_collection(paths=processed_paths,
    266                                    path_coordinates=path_coords,
    267                                    path_transforms=path_transforms,
   (...)
    271                                    styles=styles,
    272                                    mplobj=collection)

AttributeError: 'PatchCollection' object has no attribute 'get_offset_position'

This seems to be related to the plotly issue described here: https://github.com/plotly/plotly.py/issues/3624, but I have no idea how to fix this.

My environment looks like this:

> pip show mplleaflet
Name: mplleaflet
Version: 0.0.5

> pip show matplotlib
Name: matplotlib
Version: 3.5.1

Help is much appreciated. Thanks!

juanitorduz commented 2 years ago

Hey! I'm the author of the blog 👋 ! Unfortunately, I'm facing the same issue and I have not been able to resolve it. Any tip or hint is highly appreciated 😄 !

GabrielJunqueira commented 1 year ago

Hey, any update on this? I'm facing the same issue here.

gilles75-LDG commented 1 year ago

I should preface by saying that I am not software developer, just someone who thought I could use this package in my work. I don't know if this will be helpful to anyone, but I got around this and was able to make a plot by first commenting out the offset_order line in mplexporter/exporter.py, and then in the following line setting the offset_order argument equal to False.

` # offset_order = offset_dict[collection.get_offset_position()]

    self.renderer.draw_path_collection(paths=processed_paths,
                                       path_coordinates=path_coords,
                                       path_transforms=path_transforms,
                                       offsets=offsets,
                                       offset_coordinates=offset_coords,
                                       offset_order=False,
                                       styles=styles,
                                       mplobj=collection)

` I'm sure this breaks some other functionality (the points I'm scattering no longer maintain the same order as in the matplotlib scatter), but at least I could plot points on a map.