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

BUG: Fix broken call to ax.legend #2886

Merged
merged 4 commits into from
May 3, 2023

Conversation

wnavarre
Copy link
Contributor

@wnavarre wnavarre commented May 1, 2023

It seems we are getting tripped up by ax.legend's rather complex calling convention, resulting in an error: "You have mixed positional and keyword arguments, some input may be discarded."

Simplify the call by integrating all arguments into kwargs.

It seems we are getting tripped up by ax.legend's rather complex calling convention, resulting in an error:
"You have mixed positional and keyword arguments, some input may be discarded."

Simplify the call by integrating all arguments into kwargs.
@wnavarre
Copy link
Contributor Author

wnavarre commented May 2, 2023

Tests failed, but it seems possible they weren't caused by my commit since the master branch is also failing.

I rebased off of the commit described below. This seems to be the most recent commit that passed CI on master.

commit e32573d
Author: NoharaMasato 38820014+NoharaMasato@users.noreply.github.com
Date: Tue Apr 25 11:30:59 2023 +0900

@martinfleis
Copy link
Member

It seems we are getting tripped up by ax.legend's rather complex calling convention, resulting in an error: "You have mixed positional and keyword arguments, some input may be discarded."

Where do you see this error? Do you have a reproducible example?

The fix looks okay to me but I'd like to see the bug first :).

@wnavarre
Copy link
Contributor Author

wnavarre commented May 2, 2023

Yes. Bug occurs with when using "labels" in the legend_kwds dict argument to plot. Example code below.

The output in the terminal for this code is:

/home/navarre/micromamba/envs/geo_env/lib/python3.8/site-packages/geopandas/plotting.py:944: UserWarning: You have mixed positional and keyword arguments, some input may be discarded.
  ax.legend(patches, categories, **legend_kwds)
See the problem?
Better?

And the failing and succeding maps (note the weird little box in the upper left of the failing map):
failing
succeeding

import geopandas
import matplotlib.pyplot as plt

path_to_data = geopandas.datasets.get_path("nybb")
gdf = geopandas.read_file(path_to_data)

def go(ax):
    gdf.plot(column="BoroName",
             ax=ax,
             categorical=True,
             categories=["Manhattan",
                         "Bronx",
                         "Staten Island",
                         "Brooklyn",
                         "Queens"],
             legend=True,
             legend_kwds= {
                 "labels":["New York County",
                           "Bronx County",
                           "Richmond County",
                           "Kings County",
                           "Queens County"],
                 "loc": "upper left"
             })

fig, ax = plt.subplots(figsize = (10,10))
go(ax)
fig.show()
input("See the problem?")

# You've seen the problem. Now for demonstrating the solution:
    
def make_custom_legend_method(ax):
    normal_method = ax.legend
    def proposed_fix(*args, **kwargs):
        if len(args) != 2:
            # The proposed fix is on a callsite where args always equaled two...
            # Just behave as normal.
            return normal_method(*args, **kwargs)
        handles, labels = args
        kwargs.setdefault("handles", handles)
        kwargs.setdefault("labels", labels)
        normal_method(**kwargs)
    return proposed_fix

fig, ax = plt.subplots(figsize = (10,10))
ax.legend = make_custom_legend_method(ax)
go(ax)
fig.show()
input("Better?")

# Better?

@martinfleis
Copy link
Member

Ah! Great, thanks!

We should probably also add a test for this case as this apparent failure was not caught by our current test suite. Could you add one? There'll be some other testing the legend, so you can mirror the implementation.

@wnavarre
Copy link
Contributor Author

wnavarre commented May 2, 2023

Ok. Edited a test case to hit this problem.

William Navarre and others added 2 commits May 2, 2023 18:14
@martinfleis martinfleis added this to the 0.13 milestone May 3, 2023
Copy link
Member

@martinfleis martinfleis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! the CI failure is not related.

@martinfleis martinfleis merged commit a905044 into geopandas:main May 3, 2023
martinfleis added a commit to martinfleis/geopandas that referenced this pull request May 3, 2023
martinfleis added a commit that referenced this pull request May 4, 2023
JohnMoutafis pushed a commit to JohnMoutafis/geopandas that referenced this pull request Nov 16, 2023
Co-authored-by: William Navarre <navarre@dimins.com>
Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
JohnMoutafis pushed a commit to JohnMoutafis/geopandas that referenced this pull request Nov 16, 2023
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 this pull request may close these issues.

None yet

2 participants