Skip to content

legend marker update bug #2035

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

Closed
gregorio-bastardo opened this issue May 21, 2013 · 8 comments
Closed

legend marker update bug #2035

gregorio-bastardo opened this issue May 21, 2013 · 8 comments

Comments

@gregorio-bastardo
Copy link

When a line object is added to the legend without marker, the update works fine both on data and legend line, but with marker it does not refresh the legend line's marker:


import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot(range(10), range(10))

# uncomment next line to reproduce bug
# line.set_marker('d')

legend = ax.legend([line], ['foo'])
legend_line, = legend.get_lines()
fig.show()
raw_input('press enter to clear marker')
line.set_marker('')
legend_line.set_marker('')
fig.canvas.draw()
raw_input('press enter to set marker')
line.set_marker('d')
legend_line.set_marker('d')
fig.canvas.draw()
raw_input('press enter to exit')
 

I understand that lines and markers are drawn separately to support special legend patterns, but this is somehow not the case when line is originally added to the legend without marker. Please align legend line and marker property updates.

python 2.7.4 win32
matplotlib 1.2.1

originally reported on mpl-users:
http://matplotlib.1069221.n5.nabble.com/legend-marker-update-problem-tc41070.html

@tacaswell
Copy link
Member

This is related to #2532 and the fact that only one artists is returned from the legend key.

@petehuang
Copy link
Contributor

Reproduced on python 2.7.12, matplotlib 1.5.3

Thanks for the excellent code to repro

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@eltrompetero
Copy link

Is this still a bug? I'm using v3.0.1 with Python 3.6.6 and I'm finding that the marker color doesn't update.

import matplotlib.pyplot as plt
fig,ax=plt.subplots()

ax.plot([0,1],[0,1],'o-' )
leg=ax.legend((1,))
leg.legendHandles[0].set_color('k')
leg.legendHandles[0].set_mfc('k')

@ImportanceOfBeingErnest
Copy link
Member

I suppose this would be fixed by #11358, which is still work in progress.

If you are really in desperate need to do the manipulation without going through the legend creation (again), you may crawl your way through the packers and set the colors of the artists directly.

import matplotlib.pyplot as plt
fig,ax=plt.subplots()

ax.plot([0,1],[0,1],'o-' )
leg=ax.legend((1,))

c = leg.get_children()[0].get_children()[1].get_children()[0].get_children()[0].get_children()[0]
for a in c.get_children():
    a.set_color('k')
plt.show()

@eltrompetero
Copy link

Yes, I have a simpler workaround by just plotting the marker styles I want some where far away on the graph and fixing the axis limits. But I probably spent more time figuring out that this was a bug and not me being confused than hacking it....haha...

@ImportanceOfBeingErnest
Copy link
Member

@eltrompetero Sorry, I should have been clearer in my last comment. The workaround is meant for cases where you want to manipulate the legend after it being created. This might be useful in cases as the original issue, or when you have complex functions you don't want to change for the purpose, or when working with downstream libraries which hide the legend creation from you like seaborn, pandas etc.

If, however, you have full control over the legend generation, you would just create the legend with the handle you want to show. This is (hopefully) described in the legend guide. So in your case from above, you would create the legend with a black line from the start:

import matplotlib.pyplot as plt
fig,ax=plt.subplots()

ax.plot([0,1],[0,1],'o-' )
ax.legend(handles=[plt.Line2D([],[], marker="o", color="k")], labels=(1,))

That being said, if you have a suggestion on how to improve the legend guide or other documentation, which would have prevented you from spending too much time on this, we're all ears.

@eltrompetero
Copy link

Okay. Thanks! Didn't read your comment carefully enough.

@teto
Copy link

teto commented Jul 17, 2019

Any patch available ? My program relies on pandas/matplotlib and the legend is systematically wrong because of this.

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

Successfully merging a pull request may close this issue.

7 participants