Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit53faed9

Browse files
authored
Merge pull request#16864 from timhoffm/legend-labels-str2
Check parameter type for legend(labels)
2 parentsda6dfab +a1a99cd commit53faed9

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

‎examples/text_labels_and_annotations/custom_legends.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333

3434
fig,ax=plt.subplots()
3535
lines=ax.plot(data)
36-
ax.legend(lines)
36+
ax.legend()
3737

3838
##############################################################################
39-
# Note thatone legenditem per line was created.
39+
# Note thatno legendentries were created.
4040
# In this case, we can compose a legend using Matplotlib objects that aren't
4141
# explicitly tied to the data that was plotted. For example:
4242

‎lib/matplotlib/legend.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,10 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
11631163
# One argument. User defined labels - automatic handle detection.
11641164
eliflen(args)==1:
11651165
labels,=args
1166+
ifany(isinstance(l,Artist)forlinlabels):
1167+
raiseTypeError("A single argument passed to legend() must be a "
1168+
"list of labels, but found an Artist in there.")
1169+
11661170
# Get as many handles as there are labels.
11671171
handles= [handleforhandle,label
11681172
inzip(_get_legend_handles(axs,handlers),labels)]

‎lib/matplotlib/tests/test_legend.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ def test_legend_handle_label(self):
234234
plt.legend(lines, ['hello world'])
235235
Legend.assert_called_with(plt.gca(),lines, ['hello world'])
236236

237+
deftest_legend_handles_only(self):
238+
lines=plt.plot(range(10))
239+
withpytest.raises(TypeError,match='but found an Artist'):
240+
# a single arg is interpreted as labels
241+
# it's a common error to just pass handles
242+
plt.legend(lines)
243+
237244
deftest_legend_no_args(self):
238245
lines=plt.plot(range(10),label='hello world')
239246
withmock.patch('matplotlib.legend.Legend')asLegend:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp