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

ENH: Change logging to warning when creating a legend with no labels #27172

Merged
merged 7 commits into from
Dec 1, 2023
Merged

ENH: Change logging to warning when creating a legend with no labels #27172

merged 7 commits into from
Dec 1, 2023

Conversation

Jacob-Stevens-Haas
Copy link
Contributor

@Jacob-Stevens-Haas Jacob-Stevens-Haas commented Oct 23, 2023

Fixes #27145

Also add test_legend_nolabels_warning()

PR summary

After discussion in the linked issue, it was determined that warnings.warn() was better than log.warning() for alerting users that we can't draw a legend if there are no labels. This PR implements the change from log.warning() to warnings.warn (actually, _api.warn_external())

PR checklist

@Jacob-Stevens-Haas

This comment was marked as outdated.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@Jacob-Stevens-Haas

This comment was marked as duplicate.

@Jacob-Stevens-Haas Jacob-Stevens-Haas marked this pull request as ready for review October 24, 2023 12:32
@Jacob-Stevens-Haas Jacob-Stevens-Haas marked this pull request as draft October 24, 2023 12:34
@Jacob-Stevens-Haas
Copy link
Contributor Author

Jacob-Stevens-Haas commented Oct 24, 2023

Ok, I think I'm at a place I need help from a reviewer.

  • Only CI failure is in coverage of test suite, and only where there's some condition dealing with localization. [How] should I deal with this?
  • Does "New Features and API Changes are noted with a directive and release note" apply to this change?
  • Is the wording in Axes docstring and wording/change in galleries/examples/text_labels_and_annotations/custom_legends.py sensible?

@Jacob-Stevens-Haas Jacob-Stevens-Haas marked this pull request as ready for review October 24, 2023 14:26
Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

You can ignore coverage. It's sometimes doing funny things because of our CI setup.

lib/matplotlib/tests/test_legend.py Outdated Show resolved Hide resolved
lib/matplotlib/axes/_axes.py Outdated Show resolved Hide resolved
Also:
DOC: Correct Axes docstring when no labels in legend.
@Jacob-Stevens-Haas
Copy link
Contributor Author

Hey is there anything that I need to do to merge this?

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Sorry this slipped through. Thanks for the reminder.

@@ -1072,7 +1073,7 @@ def test_legend_labelcolor_rcparam_markerfacecolor_short():


def test_get_set_draggable():
legend = plt.legend()
legend = plt.legend(labels=["mock data"])
Copy link
Member

@timhoffm timhoffm Nov 13, 2023

Choose a reason for hiding this comment

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

I'm somewhat uneasy about this pattern of providing labels when there is no data.

  1. Passing labels only is supported but discouraged.
  2. While currently supported, it feels, that passing more labels than we have data should possibly warn as well. At least it's non-sensical. - But that'd be a separate PR. For here, we should just not use this pattern.

I suggest to silence the warning explicitly rather than through a sketchy code construct: Decorate the function with

@pytest.mark.filterwarnings("ignore:No artists with labels found to put in legend")

instead.

Also in the following test functions.

plt.plot([1, 2, 3])
with warnings.catch_warnings():
warnings.simplefilter("ignore")
plt.legend()
Copy link
Member

Choose a reason for hiding this comment

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

Let's at least make sure, a legend is created and attached to the Axes:

Suggested change
plt.legend()
plt.legend()
assert plt.gca().get_legend() is not None

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, makes sense! In addition, is pattern you suggested above of @pytest.mark.filterwarnings(...) preferable to warnings.catch_warnings()+simplefilter pattern?

Also assert that a legend is at least created
@timhoffm timhoffm merged commit ee241ad into matplotlib:main Dec 1, 2023
40 checks passed
@timhoffm
Copy link
Member

timhoffm commented Dec 1, 2023

Thanks, and congratulations on your first contribution to Matplotlib! We hope to see you back. 😄

@QuLogic QuLogic added this to the v3.9.0 milestone Dec 1, 2023
@Jacob-Stevens-Haas Jacob-Stevens-Haas deleted the 27145-convert-legend-warning branch December 6, 2023 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

[ENH]: Make "No artists with labels found to put in legend" a warning
5 participants