-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
FIX: legend handler warning too liberal #23762
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
Conversation
lib/matplotlib/tests/test_legend.py
Outdated
# this should _not_ warn: | ||
f, ax = plt.subplots() | ||
ax.pcolormesh(np.random.uniform(0, 1, (10, 10))) | ||
ax.get_legend_handles_labels() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need to be recording the warnings on this line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR is correct, see https://stackoverflow.com/a/45671804
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timhoffm I think you're commenting on an outdated review, the previous version had no assertions at all.
lib/matplotlib/legend.py
Outdated
@@ -1140,7 +1140,7 @@ def _get_legend_handles(axs, legend_handler_map=None): | |||
label = handle.get_label() | |||
if label != '_nolegend_' and has_handler(handler_map, handle): | |||
yield handle | |||
elif (label not in ['_nolegend_', ''] and | |||
elif (label != '' and label[0] != '_' and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may make sense to create a function _is_active_label(label)
or something. For comparsion, see line 1160 below where the same condition is written completely different.
But for 3.6 this is probably OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you simplify this by writing not label.startwith('_')
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied the code below, which is indeed identical... (had to check if an empty string was indeed False)
17482b7
to
7805ac8
Compare
anyone can merged on green. |
…762-on-v3.6.x Backport PR #23762 on branch v3.6.x (FIX: legend handler warning too liberal)
I'm curious why this didn't end up in the 3.6.0rc1 release? (https://github.com/matplotlib/matplotlib/blob/v3.6.0rc1/lib/matplotlib/legend.py#L1143) |
Because 3.6.0rc1 came out before this was even opened. |
PR Summary
Closes #23761
The check is not supposed to warn if an artist has no label or a
'_no_legend_'
label. However we actually add labels to lots of artists, and we don't deal with labels with leading underscores anyway, so just skip anything with a leading underscore.PR Checklist
Tests and Styling
pytest
passes).flake8-docstrings
and runflake8 --docstring-convention=all
).Documentation
doc/users/next_whats_new/
(follow instructions in README.rst there).doc/api/next_api_changes/
(follow instructions in README.rst there).