Skip to content

Passing legend=True to plot_animated on a GeoDataFrame creates one legend for each frame #38

@hraftery

Description

@hraftery

Describe the bug
Creating a GeoDataFrame using geopandas in a format suitable for plot_animated applies a matplotlib colour map to each frame. In matplotlib, to display the colour map on the graph you turn on the "legend". This can be done by passing legend=True as an argument to the plot method.

pandas_alive permits supplying extra arguments to plot_animated which are passed on to plot. Unfortunately, matplotlib draws the colour map legend as another axis, so when used in plot_animated, each successive frame gets an additional colour map, ruining the effect.

To Reproduce
This is as per the documention, with the addition of legend=True

import geopandas
import pandas_alive
import contextily

gdf = geopandas.read_file('data/italy-covid-region.gpkg')
gdf.index = gdf.region
gdf = gdf.drop('region',axis=1)

map_chart = gdf.plot_animated(filename='examples/example-geo-polygon-chart.gif',basemap_format={'source':contextily.providers.Stamen.Terrain}, legend=True)

Expected behavior
The legend to appear in the animation, as it does for a single call to plot.

Screenshots

Screen Shot 2021-10-10 at 4 22 46 pm

Additional context
The following diff on pandas_alive/geocharts.py fixes the issue:

184a185,191
>         # Added by HR211010. If the user passes "legend=True", only apply to first frame.
>         if i == 0:
>             try:
>                 self.kwargs.pop("legend")
>             except KeyError:
>                 pass
> 

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions