-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ENH: Change logging to warning when creating a legend with no labels #27172
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
Changes from 4 commits
46286fa
f53b9d9
a413a02
bc1054b
9f80d55
04e0ceb
dbe82fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -636,6 +636,7 @@ def test_handler_numpoints(): | |||||||
def test_text_nohandler_warning(): | ||||||||
"""Test that Text artists with labels raise a warning""" | ||||||||
fig, ax = plt.subplots() | ||||||||
ax.plot([0], label="mock data") | ||||||||
ax.text(x=0, y=0, s="text", label="label") | ||||||||
with pytest.warns(UserWarning) as record: | ||||||||
ax.legend() | ||||||||
|
@@ -703,7 +704,7 @@ def test_legend_title_empty(): | |||||||
# it comes back as an empty string, and that it is not | ||||||||
# visible: | ||||||||
fig, ax = plt.subplots() | ||||||||
ax.plot(range(10)) | ||||||||
ax.plot(range(10), label="mock data") | ||||||||
leg = ax.legend() | ||||||||
assert leg.get_title().get_text() == "" | ||||||||
assert not leg.get_title().get_visible() | ||||||||
|
@@ -736,7 +737,7 @@ def test_window_extent_cached_renderer(): | |||||||
|
||||||||
def test_legend_title_fontprop_fontsize(): | ||||||||
# test the title_fontsize kwarg | ||||||||
plt.plot(range(10)) | ||||||||
plt.plot(range(10), label="mock data") | ||||||||
with pytest.raises(ValueError): | ||||||||
plt.legend(title='Aardvark', title_fontsize=22, | ||||||||
title_fontproperties={'family': 'serif', 'size': 22}) | ||||||||
|
@@ -747,27 +748,27 @@ def test_legend_title_fontprop_fontsize(): | |||||||
|
||||||||
fig, axes = plt.subplots(2, 3, figsize=(10, 6)) | ||||||||
axes = axes.flat | ||||||||
axes[0].plot(range(10)) | ||||||||
axes[0].plot(range(10), label="mock data") | ||||||||
leg0 = axes[0].legend(title='Aardvark', title_fontsize=22) | ||||||||
assert leg0.get_title().get_fontsize() == 22 | ||||||||
axes[1].plot(range(10)) | ||||||||
axes[1].plot(range(10), label="mock data") | ||||||||
leg1 = axes[1].legend(title='Aardvark', | ||||||||
title_fontproperties={'family': 'serif', 'size': 22}) | ||||||||
assert leg1.get_title().get_fontsize() == 22 | ||||||||
axes[2].plot(range(10)) | ||||||||
axes[2].plot(range(10), label="mock data") | ||||||||
mpl.rcParams['legend.title_fontsize'] = None | ||||||||
leg2 = axes[2].legend(title='Aardvark', | ||||||||
title_fontproperties={'family': 'serif'}) | ||||||||
assert leg2.get_title().get_fontsize() == mpl.rcParams['font.size'] | ||||||||
axes[3].plot(range(10)) | ||||||||
axes[3].plot(range(10), label="mock data") | ||||||||
leg3 = axes[3].legend(title='Aardvark') | ||||||||
assert leg3.get_title().get_fontsize() == mpl.rcParams['font.size'] | ||||||||
axes[4].plot(range(10)) | ||||||||
axes[4].plot(range(10), label="mock data") | ||||||||
mpl.rcParams['legend.title_fontsize'] = 20 | ||||||||
leg4 = axes[4].legend(title='Aardvark', | ||||||||
title_fontproperties={'family': 'serif'}) | ||||||||
assert leg4.get_title().get_fontsize() == 20 | ||||||||
axes[5].plot(range(10)) | ||||||||
axes[5].plot(range(10), label="mock data") | ||||||||
leg5 = axes[5].legend(title='Aardvark') | ||||||||
assert leg5.get_title().get_fontsize() == 20 | ||||||||
|
||||||||
|
@@ -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"]) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
I suggest to silence the warning explicitly rather than through a sketchy code construct: Decorate the function with
instead. Also in the following test functions. |
||||||||
assert not legend.get_draggable() | ||||||||
legend.set_draggable(True) | ||||||||
assert legend.get_draggable() | ||||||||
|
@@ -1289,74 +1290,75 @@ def test_loc_invalid_tuple_exception(): | |||||||
fig, ax = plt.subplots() | ||||||||
with pytest.raises(ValueError, match=('loc must be string, coordinate ' | ||||||||
'tuple, or an integer 0-10, not \\(1.1,\\)')): | ||||||||
ax.legend(loc=(1.1, )) | ||||||||
ax.legend(loc=(1.1, ), labels=["mock data"]) | ||||||||
|
||||||||
with pytest.raises(ValueError, match=('loc must be string, coordinate ' | ||||||||
'tuple, or an integer 0-10, not \\(0.481, 0.4227, 0.4523\\)')): | ||||||||
ax.legend(loc=(0.481, 0.4227, 0.4523)) | ||||||||
ax.legend(loc=(0.481, 0.4227, 0.4523), labels=["mock data"]) | ||||||||
|
||||||||
with pytest.raises(ValueError, match=('loc must be string, coordinate ' | ||||||||
'tuple, or an integer 0-10, not \\(0.481, \'go blue\'\\)')): | ||||||||
ax.legend(loc=(0.481, "go blue")) | ||||||||
ax.legend(loc=(0.481, "go blue"), labels=["mock data"]) | ||||||||
|
||||||||
|
||||||||
def test_loc_valid_tuple(): | ||||||||
fig, ax = plt.subplots() | ||||||||
ax.legend(loc=(0.481, 0.442)) | ||||||||
ax.legend(loc=(1, 2)) | ||||||||
ax.legend(loc=(0.481, 0.442), labels=["mock data"]) | ||||||||
ax.legend(loc=(1, 2), labels=["mock data"]) | ||||||||
|
||||||||
|
||||||||
def test_loc_valid_list(): | ||||||||
fig, ax = plt.subplots() | ||||||||
ax.legend(loc=[0.481, 0.442]) | ||||||||
ax.legend(loc=[1, 2]) | ||||||||
ax.legend(loc=[0.481, 0.442], labels=["mock data"]) | ||||||||
ax.legend(loc=[1, 2], labels=["mock data"]) | ||||||||
|
||||||||
|
||||||||
def test_loc_invalid_list_exception(): | ||||||||
fig, ax = plt.subplots() | ||||||||
with pytest.raises(ValueError, match=('loc must be string, coordinate ' | ||||||||
'tuple, or an integer 0-10, not \\[1.1, 2.2, 3.3\\]')): | ||||||||
ax.legend(loc=[1.1, 2.2, 3.3]) | ||||||||
ax.legend(loc=[1.1, 2.2, 3.3], labels=["mock data"]) | ||||||||
|
||||||||
|
||||||||
def test_loc_invalid_type(): | ||||||||
fig, ax = plt.subplots() | ||||||||
with pytest.raises(ValueError, match=("loc must be string, coordinate " | ||||||||
"tuple, or an integer 0-10, not {'not': True}")): | ||||||||
ax.legend(loc={'not': True}) | ||||||||
ax.legend(loc={'not': True}, labels=["mock data"]) | ||||||||
|
||||||||
|
||||||||
def test_loc_validation_numeric_value(): | ||||||||
fig, ax = plt.subplots() | ||||||||
ax.legend(loc=0) | ||||||||
ax.legend(loc=1) | ||||||||
ax.legend(loc=5) | ||||||||
ax.legend(loc=10) | ||||||||
ax.legend(loc=0, labels=["mock data"]) | ||||||||
ax.legend(loc=1, labels=["mock data"]) | ||||||||
ax.legend(loc=5, labels=["mock data"]) | ||||||||
ax.legend(loc=10, labels=["mock data"]) | ||||||||
with pytest.raises(ValueError, match=('loc must be stri 1E0A ng, coordinate ' | ||||||||
'tuple, or an integer 0-10, not 11')): | ||||||||
ax.legend(loc=11) | ||||||||
ax.legend(loc=11, labels=["mock data"]) | ||||||||
|
||||||||
with pytest.raises(ValueError, match=('loc must be string, coordinate ' | ||||||||
'tuple, or an integer 0-10, not -1')): | ||||||||
ax.legend(loc=-1) | ||||||||
ax.legend(loc=-1, labels=["mock data"]) | ||||||||
|
||||||||
|
||||||||
def test_loc_validation_string_value(): | ||||||||
fig, ax = plt.subplots() | ||||||||
ax.legend(loc='best') | ||||||||
ax.legend(loc='upper right') | ||||||||
ax.legend(loc='best') | ||||||||
ax.legend(loc='upper right') | ||||||||
ax.legend(loc='upper left') | ||||||||
ax.legend(loc='lower left') | ||||||||
ax.legend(loc='lower right') | ||||||||
ax.legend(loc='right') | ||||||||
ax.legend(loc='center left') | ||||||||
ax.legend(loc='center right') | ||||||||
ax.legend(loc='lower center') | ||||||||
ax.legend(loc='upper center') | ||||||||
labels = ["mock data"] | ||||||||
ax.legend(loc='best', labels=labels) | ||||||||
ax.legend(loc='upper right', labels=labels) | ||||||||
ax.legend(loc='best', labels=labels) | ||||||||
ax.legend(loc='upper right', labels=labels) | ||||||||
ax.legend(loc='upper left', labels=labels) | ||||||||
ax.legend(loc='lower left', labels=labels) | ||||||||
ax.legend(loc='lower right', labels=labels) | ||||||||
ax.legend(loc='right', labels=labels) | ||||||||
ax.legend(loc='center left', labels=labels) | ||||||||
ax.legend(loc='center right', labels=labels) | ||||||||
ax.legend(loc='lower center', labels=labels) | ||||||||
ax.legend(loc='upper center', labels=labels) | ||||||||
with pytest.raises(ValueError, match="'wrong' is not a valid value for"): | ||||||||
ax.legend(loc='wrong') | ||||||||
ax.legend(loc='wrong', labels=labels) | ||||||||
|
||||||||
|
||||||||
def test_legend_handle_label_mismatch(): | ||||||||
|
@@ -1375,3 +1377,9 @@ def test_legend_handle_label_mismatch_no_len(): | |||||||
labels=iter(["pl1", "pl2", "pl3"])) | ||||||||
assert len(legend.legend_handles) == 2 | ||||||||
assert len(legend.get_texts()) == 2 | ||||||||
|
||||||||
|
||||||||
def test_legend_nolabels_warning(): | ||||||||
plt.plot([1, 2, 3]) | ||||||||
with pytest.raises(UserWarning): | ||||||||
timhoffm marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
plt.legend() | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha, makes sense! In addition, is pattern you suggested above of |
Uh oh!
There was an error while loading. Please reload this page.