X Tutup
Skip to content

Don't warn when legend() finds no labels.#6753

Merged
QuLogic merged 1 commit intomatplotlib:masterfrom
anntzer:dont-warn-on-empty-legend
Sep 30, 2016
Merged

Don't warn when legend() finds no labels.#6753
QuLogic merged 1 commit intomatplotlib:masterfrom
anntzer:dont-warn-on-empty-legend

Conversation

@anntzer
Copy link
Copy Markdown
Contributor

@anntzer anntzer commented Jul 16, 2016

There's no warning when calling plot([]), so I don't see why there should be
one when calling legend() with no labeled artists either (except for catching
basic bugs where someone forgets to pass label=..., but such errors are
pretty obvious visually anyways and the warning doesn't help when only some
labels are missing).

Typical use case: making a complicated plot with a lot of elements which each
may or may not have a label; now I need to separately keep track of whether I
actually did add a label before deciding whether to call legend() at the
end.

@efiring
Copy link
Copy Markdown
Member

efiring commented Jul 16, 2016

I agree with the rationale here. Is there, or should there be, a simple test to ensure that calling legend with no labels works correctly?

@anntzer
Copy link
Copy Markdown
Contributor Author

anntzer commented Jul 16, 2016

I can add a test that just does ax.plot([]); ax.legend() and does not assert anything. Would that be enough?

@efiring
Copy link
Copy Markdown
Member

efiring commented Jul 16, 2016

Yes.
Thinking again about the rationale, I am less sure, though still mildly in favor: in the use case you mention, the present result is a warning, which doesn't actually require you to keep track of whether there is anything to go in the legend.

@tacaswell
Copy link
Copy Markdown
Member

I suspect the reason this is here is to deal with the case of

ax.plot(x, y)
ax.legend()
# MATPLOTLIB LEGENDS ARE BROKEN!!!!!1!

style bug reports.

@anntzer
Copy link
Copy Markdown
Contributor Author

anntzer commented Jul 16, 2016

Well, a warning you know to be spurious is still a (minor) annoyance. Another option would be to add an allow_empty kwarg to the (already long, but whatever) signature of legend(), defaulting to False.

@efiring
Copy link
Copy Markdown
Member

efiring commented Jul 16, 2016

A kwarg to defeat a warning? No, I would prefer that we decide: warn, or don't warn.

@tacaswell tacaswell modified the milestone: 2.1 (next point release) Jul 16, 2016
@WeatherGod
Copy link
Copy Markdown
Member

Agreed. No kwarg. If a user expects a warning and doesn't want it, they can
use a context manager to catch them.

On Sat, Jul 16, 2016 at 2:54 PM, Eric Firing notifications@github.com
wrote:

A kwarg to defeat a warning? No, I would prefer that we decide: warn, or
don't warn.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#6753 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AARy-J0K74GcT_xA6oCNM82f2HxcEUpuks5qWSjOgaJpZM4JN8wQ
.

@fariza
Copy link
Copy Markdown
Member

fariza commented Sep 29, 2016

I agree, the warning is pretty annoying
👍

@tacaswell
Copy link
Copy Markdown
Member

👍 on removing the warning.

Can you add an entry in whats_new?

There's no warning when calling `plot([])`, so I don't see why there should be
one when calling `legend()` with no labeled artists either (except for catching
basic bugs where someone forgets to pass `label=...`, but such errors are
pretty obvious visually anyways and the warning doesn't help when only *some*
labels are missing).

Typical use case: making a complicated plot with a lot of elements which each
may or may not have a label; now I need to separately keep track of whether I
actually *did* add a label before deciding whether to call `legend()` at the
end.
@anntzer anntzer force-pushed the dont-warn-on-empty-legend branch from 8daaf5e to 2fd8362 Compare September 29, 2016 16:17
@anntzer
Copy link
Copy Markdown
Contributor Author

anntzer commented Sep 29, 2016

done.

@NelleV NelleV changed the title Don't warn when legend() finds no labels. [MRG+1] Don't warn when legend() finds no labels. Sep 30, 2016
@QuLogic QuLogic changed the title [MRG+1] Don't warn when legend() finds no labels. Don't warn when legend() finds no labels. Sep 30, 2016
@QuLogic QuLogic merged commit 45dbc9b into matplotlib:master Sep 30, 2016
@anntzer anntzer deleted the dont-warn-on-empty-legend branch September 30, 2016 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

X Tutup