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

Commit40369b5

Browse files
committed
Automagically set the stacklevel on warnings.
There are many places in Matplotlib where it is impossible to set astatic stacklevel on warnings that works in all cases, because a samefunction may either be called directly or via some wrapper (e.g.pyplot).Instead, compute the stacklevel by walking the stack. Given thatwarnings refer to conditions that should, well, be avoided, I believewe don't need to worry too much about the runtime cost.As an example, use this mechanism for the "ambiguous second argument toplot" warning. Now both```plt.gca().plot("x", "y", data={"x": 1, "y": 2})```and```plt.plot("x", "y", data={"x": 1, "y": 2})```emit a warning that refers to the relevant line in the user source,whereas previously the second snippet would refer to the pyplot wrapper,which is irrelevant to the user.Note that this only works from source, not from IPython, as the latteruses `exec` and AFAICT there is no value of stacklevel that correctlyrefers to the string being exec'd.Of course, the idea would be to ultimately use this throughout thecodebase.
1 parent1e6790f commit40369b5

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

‎lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ def _plot_args_replacer(args, data):
6464
exceptValueError:
6565
pass
6666
else:
67-
warnings.warn(
67+
cbook._warn(
6868
"Second argument {!r} is ambiguous: could be a color spec but "
6969
"is in data; using as data. Either rename the entry in data "
7070
"or use three arguments to plot.".format(args[1]),
71-
RuntimeWarning,stacklevel=3)
71+
RuntimeWarning)
7272
return ["x","y"]
7373
eliflen(args)==3:
7474
return ["x","y","c"]

‎lib/matplotlib/cbook/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
importglob
1515
importgzip
1616
importio
17-
fromitertoolsimportrepeat
17+
importitertools
1818
importlocale
1919
importnumbers
2020
importoperator
@@ -1254,7 +1254,7 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
12541254

12551255
ncols=len(X)
12561256
iflabelsisNone:
1257-
labels=repeat(None)
1257+
labels=itertools.repeat(None)
12581258
eliflen(labels)!=ncols:
12591259
raiseValueError("Dimensions of labels and X must be compatible")
12601260

@@ -2032,3 +2032,16 @@ def _setattr_cm(obj, **kwargs):
20322032
delattr(obj,attr)
20332033
else:
20342034
setattr(obj,attr,orig)
2035+
2036+
2037+
def_warn(message,category=None):
2038+
"""
2039+
`warnings.warn` wrapper that defaults *stacklevel* to "outside Matplotlib".
2040+
"""
2041+
frame=sys._getframe()
2042+
forstacklevelinitertools.count(1):
2043+
ifnotre.match(r"\A(matplotlib|mpl_toolkits)(\Z|\.)",
2044+
frame.f_globals["__name__"]):
2045+
break
2046+
frame=frame.f_back
2047+
warnings.warn(message,category,stacklevel)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp