Skip to content

Conversation

mittal-aakriti
Copy link

@mittal-aakriti mittal-aakriti commented Oct 3, 2025

  • closes BUG: df.plot - Changing the legend does not work for shared_y #20183 Added 'legend_loc' parameter to the plot function in pandas that calls matplotlib's legend loc internally.
  • Tests added and passed. Fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

@mittal-aakriti mittal-aakriti marked this pull request as ready for review October 7, 2025 02:33
Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

Could you link the associated issue in #62568 (comment)?


if len(handles) > 0:
ax.legend(handles, labels, loc="best", title=title)
# ax.legend(handles, labels, loc="best", title=title)
Copy link
Member

Choose a reason for hiding this comment

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

Could you remove these commented out lines?

Copy link
Author

Choose a reason for hiding this comment

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

Updated

@mroeschke mroeschke added the Visualization plotting label Oct 7, 2025
@jbrockmendel
Copy link
Member

Is there an alternative where the user does this in post-processing? I.e. can we make this Not Our Problem?

@mittal-aakriti
Copy link
Author

mittal-aakriti commented Oct 7, 2025

The alternative is to use the ax object created by matplotlib and then change the legend location but the problem arises when we call secondary_y. Pandas internally calls matplotlib's twinx and attaches it to ax as:

  • ax, ax.right_ax: When there are few columns on secondary_y and not all. In this case you need to get the legend handles for both ax and ax.right_ax, append them and then change their location. Something like the following:
h1, l1 = ax.get_legend_handles_labels()
h2, l2 = ax.right_ax.get_legend_handles_labels()
ax.legend(h1 + h2, l1 + l2, loc='upper center')
  • ax, ax.left_ax: When all the columns are on secondary_y. In order to set the legend location correctly in this case, we will have to follow following steps:

While plotting turn off the legend by (legend=False) and then set ax.legend(loc='upper..')
However if we don't turn off the legend then the legend will be overwritten instead of changing the location

leg = ax.left_ax.get_legend()
leg.set_loc('upper center')

So just giving the legend location legend_loc as the parameter initially solves all these problems in a non-hacky way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: df.plot - Changing the legend does not work for shared_y
3 participants