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

legend handle size does not automatically scale with linewidth #8789

Closed
janssen opened this issue Jun 22, 2017 · 12 comments
Closed

legend handle size does not automatically scale with linewidth #8789

janssen opened this issue Jun 22, 2017 · 12 comments

Comments

@janssen
Copy link

janssen commented Jun 22, 2017

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:

legend-ok

And here's linewidth == 2 and various line styles:

legend-fail

Legend is drawn with

leftaxis.legend(handles=lines, loc='best', numpoints=3, frameon=True, fancybox=True, framealpha=0.5)

where lines are generated with Axes.plot.

Matplotlib version

Linux 16.04
Matplotlib 2.0.2 with Agg backend, from source
Python 2.7.13

@janssen janssen changed the title linestyles don't show in legend if linewidth != 1 linestyles don't show in legend if numpoints > 1 Jun 22, 2017
@janssen
Copy link
Author

janssen commented Jun 22, 2017

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()

@dstansby
Copy link
Member

Here's an even smaller example:

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()

plt.show()

If I'm correct, the problem is that with a large linewidth and -. only a dash is shown in the legend, and no dot.
figure_1

@janssen
Copy link
Author

janssen commented Jun 22, 2017

Haven't looked at the code, but it looks like the legend "handler box" for the legend key is zoomed by the line thickness.

@tacaswell
Copy link
Member

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 handlelength to legend

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()

so

@tacaswell tacaswell added this to the 2.2 (next next feature release) milestone Jun 22, 2017
@tacaswell tacaswell changed the title linestyles don't show in legend if numpoints > 1 legend handle size does not automatically scale with linewidth Jun 22, 2017
@janssen
Copy link
Author

janssen commented Jun 23, 2017

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?

@dopplershift
Copy link
Contributor

If you do that, your dots in your pattern start to look like sticks.

@afvincent
Copy link
Contributor

@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()

thinner_legend_handle

@afvincent
Copy link
Contributor

@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 ^^...)

@jklymak
Copy link
Member

jklymak commented Oct 17, 2017

I'm not clear on the "bug" here. That the handle length doesn't increase automagically to include the dot-dash pattern?

@afvincent
Copy link
Contributor

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.

@github-actions
Copy link

github-actions bot commented Jun 5, 2023

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!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Jun 5, 2023
@jklymak
Copy link
Member

jklymak commented Jun 5, 2023

This probably could still go n the legend guide somewhere? https://matplotlib.org/devdocs/users/explain/axes/legend_guide.html

@github-actions github-actions bot removed the status: inactive Marked by the “Stale” Github Action label Jun 7, 2023
@QuLogic QuLogic modified the milestones: future releases, v3.9.0 Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants