Skip to content

Don't use removed QuadContourSet.collections in gallery example#7638

Merged
mkcor merged 2 commits intoscikit-image:mainfrom
lagru:avoid-quadcontourset-collections
Dec 18, 2024
Merged

Don't use removed QuadContourSet.collections in gallery example#7638
mkcor merged 2 commits intoscikit-image:mainfrom
lagru:avoid-quadcontourset-collections

Conversation

@lagru
Copy link
Copy Markdown
Member

@lagru lagru commented Dec 17, 2024

Description

Fixes #7636.

This was deprecated in matplotlib 3.8. I didn't really figure out an equivalent method that can use set_label. I usually got a "Legend does not support handles for QuadContourSet" error. So instead, I used proxy artists as suggested by
https://matplotlib.org/stable/users/explain/axes/legend_guide.html#creating-artists-specifically-for-adding-to-the-legend-aka-proxy-artists

Also update the API to use "import skimage as ski".

Checklist

Release note

For maintainers and optionally contributors, please refer to the instructions on how to document this PR for the release notes.

...

This was deprecated in matplotlib 3.8. I didn't really figure out an
equivalent method that can use `set_label`. I usually got a
"Legend does not support handles for QuadContourSet" error. So instead,
I used proxy artists as suggested by
https://matplotlib.org/stable/users/explain/axes/legend_guide.html#creating-artists-specifically-for-adding-to-the-legend-aka-proxy-artists

Also update the API to use "import skimage as ski".
@lagru lagru added the 📄 type: Documentation Updates, fixes and additions to documentation label Dec 17, 2024
@lagru lagru added this to the 0.25.1 milestone Dec 17, 2024
@lagru lagru mentioned this pull request Dec 17, 2024
@lagru lagru added the ⚠️ Priority This issue or PR should be given priority label Dec 17, 2024
Copy link
Copy Markdown
Contributor

@jarrodmillman jarrodmillman left a comment

Choose a reason for hiding this comment

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

Thanks!!

Comment on lines +90 to +100
ax[1].contour(evolution[2], [0.5], colors='g')
ax[1].contour(evolution[7], [0.5], colors='y')
ax[1].contour(evolution[-1], [0.5], colors='r')

# `Axes.legend` does not support handles for `QuadContourSet`, define proxy
# artists instead
contour_labels = [
mlines.Line2D([], [], color="g", label="Iteration 2"),
mlines.Line2D([], [], color="y", label="Iteration 7"),
mlines.Line2D([], [], color="r", label="Iteration 35"),
]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we pop this into a for-loop, and simplify the comment above contour_labels? I think the current text will be a bit confusing to newcomers, one of our target audiences. Perhaps something like "placeholder labels for contours" or similar.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(I can also push the change, let me know.)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is also the legend_elements method, which essentially creates proxy artists for you.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @rcomer; I tried that, but I couldn't figure out how to make it work in our case, since set_label doesn't seem to affect the returned label elements. Is there a way to set the label on each contour that way, so it comes back via legend_elements?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don’t think there is a way to set the labels on the contours. You can pass separate lists of handles and labels to legend though. So something like

handles = [cs.legend_elements()[0][0] for cs in contour_sets]
ax[3].legend(handles=handles, labels=labels)

Whether that’s any less confusing than what you currently have, I’m not sure…

Copy link
Copy Markdown
Member

@mkcor mkcor left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this, @lagru! 🙏

@stefanv
Copy link
Copy Markdown
Member

stefanv commented Dec 17, 2024

Take a look and see if you like this version (or not :D).

@lagru
Copy link
Copy Markdown
Member Author

lagru commented Dec 18, 2024

Thanks @stefanv. I can't approve my own PR but consider it approved. ✔️ Feel welcome to merge @scikit-image/core to get the CI green again.

@mkcor mkcor merged commit 02795e2 into scikit-image:main Dec 18, 2024
@lagru lagru deleted the avoid-quadcontourset-collections branch February 4, 2025 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📄 type: Documentation Updates, fixes and additions to documentation ⚠️ Priority This issue or PR should be given priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gallery example on morphological snakes uses removed attribute 'collections.'

5 participants