Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug Fix for Legend Label Removal Issue in ErrorbarContainer Object #25396

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/matplotlib/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def remove(self):
self, scalarp=lambda x: isinstance(x, Artist)):
if c is not None:
c.remove()
self.set_label("")
Copy link
Member

Choose a reason for hiding this comment

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

I guess this is OK - it's a bit strange that legend is treating a Container as an Artist, but "remove" doesn't remove the Artist from consideration by legend. Setting the label to be empty accomplishes legend ignoring the Container, but at this point the Container should not be showing up in the list of Artists.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well, you would think, right? But I guess for some reason, it still shows up in the legend unless you specifically set the label to empty. Weird, right?

Copy link
Member

Choose a reason for hiding this comment

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

Yes - but I think this would be better if it chased down why that doesn't happen. Just blanking the label papers over the problem, but isn't a very robust fix. For instance you could imagine at some point we decide to include legend entries for artists with empty label strings.

Copy link
Contributor Author

@Higgs32584 Higgs32584 Mar 10, 2023

Choose a reason for hiding this comment

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

@jklymak, the bug has nothing to do with auto-updating; as the test cases show, the auto-updating likely has to do with the flattening of containers.

It likely has something to do with flattening the nested containers, which would probably be of greater risk for something else happening since "cbook.flatten" is referenced so much. I looked into other ways of removing it from the legend, but it seems there are stringent Object-Oriented protocols that I did not want to violate in my pull request. This seemed most in line with the current syntax of the rest of the function. If you query how many labels are in the value, the label is also absent, and the length is one, so there is not just a blank over.

Copy link
Member

Choose a reason for hiding this comment

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

My point is that the container is apparently being left in the list of artists on the axis, and it should not be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I understand. Where would you recommend looking?

Copy link
Member

Choose a reason for hiding this comment

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

I'd look at how we get the list of handles in _get_legend_handles and decide why the the Collection has not been removed from ax._children. Note I'm not suggesting changing anything in legend, probably just in Collection.remove

Copy link
Member

Choose a reason for hiding this comment

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

Looking at it more, I'd just put a bunch of print statements in container.remvoe and try and figure out why self._remove_method(self) is either not being called or not working

if self._remove_method:
self._remove_method(self)

Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,19 @@ def test_errorbar_remove():

eb = ax.errorbar([1], [1], fmt='none')
eb.remove()


def test_removal_from_legend():
ax = plt.gca()

obj1 = ax.errorbar([1], [1], [1], marker="o", label="foo")

obj1.remove()

ax.errorbar([1.1], [1.1], [1], marker="o", label="bar")

handles, labels = ax.get_legend_handles_labels()

assert "foo" not in labels

assert len(handles) == 1