-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Plot area legend rendered incorrectly when moved out of the plot #13544
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
Comments
IIRC the area plot legend is a bit of a hack. It's done here Interested in seeing how much it would be to get this working? |
Yes, I was already digging pandas' source and arrived at the conclusion that the function you pointed out is the one wihch builds the legend, at least when it is placed at its default location. However, for some reason which I don't understand (since I'm not familiar with pandas nor matplotlib internal architecture), that is not the one used when you use the method I tried to study the code of Some hints about how to debug this issue? |
I've found a workaround, just in case anyone has the same problem: # Generate and plot the areas
df = pd.DataFrame(np.random.rand(20, 5), columns=['A', 'B', 'C', 'D', 'E'])
a = df.plot(kind='area', linewidth=0.1, alpha=0.6)
# Recreate the legend (reversing labels to preserve the stacked order of the areas)
# and put it at the right of the plot
import matplotlib.patches as mpatches
handles, labels = a.get_legend_handles_labels()
a.legend(reversed([mpatches.Rectangle((0, 0), 1, 1, fc=handle.get_color(), alpha=handle.get_alpha())
for handle in handles]), reversed(labels),
loc="center left", bbox_to_anchor=(1.02, 0.5)) |
What is the underlying artists that |
@tacaswell it's It may be a timing to remove the legend hack which @TomAugspurger pointed, showing a warning if mpl < 1.5.0 which supports |
This issue should be closed (?) as it was addressed in the aforementioned pull. |
Code Sample, a copy-pastable example if possible
In area plots, the handles in the legend are made with filled rectangles, as for example in:
However, if method
legend()
is used to alter the position of the legend, the handles are rendered as lines, which poses a problem if the line width is small, as in the following example:output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: