-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
legend handle size does not automatically scale with linewidth #8789
Comments
Here's a test program: import matplotlib.pyplot as plt
line1, = plt.plot([1,2,3], label="Line 1", linewidth=1, linestyle='--')
line2, = plt.plot([3,2,1], label="Line 2", linewidth=1, linestyle='-.')
line3, = plt.plot([2,2,2], label="Line 2", linewidth=1, linestyle='-')
line4, = plt.plot([1.5,2,2.5], label="Line 1", linewidth=2, linestyle='--')
line5, = plt.plot([2.5,2,1.5], label="Line 2", linewidth=2, linestyle='-.')
line6, = plt.plot([0.75,2,2.75], label="Line 2", linewidth=2, linestyle='-')
# Create another legend for the second line.
plt.legend(handles=[line1, line2, line3, line4, line5, line6], loc='best', numpoints=2)
plt.show() |
Haven't looked at the code, but it looks like the legend "handler box" for the legend key is zoomed by the line thickness. |
The dash-pattern is scaled by line width, the length of the line in the legend is not so it is too short to see the full pattern. The work-around for now is to pass import matplotlib.pyplot as plot
fig, ax = plt.subplots()
ax.plot([1,2], linestyle='-.', linewidth=1, label='1')
ax.plot([1,2], linestyle='-.', linewidth=3, label='3')
ax.legend(handlelength=5)
plt.show() |
Thanks, that helps. I suppose it's not possible to only scale the handle in one dimension? That is, keep the handles the same length, but thicken them appropriately? |
If you do that, your dots in your pattern start to look like sticks. |
@janssen A possible workaround to achieve something close to what you are looking for might be to alter the linewidth of the legend handles after the legend is created. Something like: import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2], linestyle='-.', linewidth=1, label='Entry #1')
ax.plot([2, 3], linestyle='-.', linewidth=3, label='Entry #2')
leg = ax.legend()
# Make the handle of the 2nd legend entry thinner (lw = 1 instead of 3)
leg.get_lines()[1].set_linewidth(1)
plt.show() |
@tacaswell What should be done about this issue? Should we fix something? Improve the documentation about custom legend maybe? (There are already so many entries in #9458 that one more or one less ^^...) |
I'm not clear on the "bug" here. That the handle length doesn't increase automagically to include the dot-dash pattern? |
IMHO the current behavior is fine, I apologize if I was not clear. Nevertheless one could still think about documenting with a dedicated example or somewhere in a tutorial how to increase the length of the handles or manually tweak their line width. |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
This probably could still go n the legend guide somewhere? https://matplotlib.org/devdocs/users/explain/axes/legend_guide.html |
Bug report
If I use a line style that's dash-dot plus a linewidth that's greater than 1, the linestyle doesn't appear in the legend if numpoints > 1.
Here's linewidth == 1 and various line styles:
And here's linewidth == 2 and various line styles:
Legend is drawn with
where
lines
are generated withAxes.plot
.Matplotlib version
Linux 16.04
Matplotlib 2.0.2 with Agg backend, from source
Python 2.7.13
The text was updated successfully, but these errors were encountered: