Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1bd1f05

Browse files
committed
Simplify _preprocess_data using Signature.bind.
Public API change: `step` no longer defaults to using `y` as label_namer. This is consistent with other functions that wrap `plot` (`plot` itself, `loglog`, etc.). (Alternatively, we could make all these functions use `y` as label_namer; I don't really care either way.) The plot-specific data kwarg logic was moved to `_process_plot_var_args`, dropping the need for general callable `positional_parameter_names`, `_plot_args_replacer`, and `positional_parameter_names`. `test_positional_parameter_names_as_function` and tests using `plot_func_varargs` were removed as a consequence. `replace_all_args` can be replaced by making `replace_names=None` trigger replacement of all args, even the "unknown" ones. There was no real use of "replace all known args but not unknown ones" (even if there was, this can easily be handled by explicitly listing the args in replace_names). `test_function_call_with_replace_all_args` was removed as a consequence. `replace_names` no longer complains if some argument names it is given are not present in the "explicit" signature, as long as the function accepts `**kwargs` -- because it may find the arguments in kwargs instead. label_namer no longer triggers if `data` is not passed (if the argument specified by label_namer was a string, then it is likely a categorical and shouldn't be considered as a label anyways). `test_label_problems_at_runtime` was renamed to `test_label_namer_only_if_data` and modified accordingly. Calling data-replaced functions used to trigger RuntimeError in some cases of mismatched arguments; they now trigger TypeError similarly to how normal functions do (`test_more_args_than_pos_parameters`).
1 parent 9595a7d commit 1bd1f05

File tree

7 files changed

+226
-435
lines changed

7 files changed

+226
-435
lines changed

doc/api/next_api_changes/2018-02-15-AL-deprecations.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ The following classes, methods, functions, and attributes are deprecated:
1313
- ``afm.parse_afm``,
1414
- ``backend_wx.FigureCanvasWx.macros``,
1515
- ``cbook.GetRealpathAndStat``, ``cbook.Locked``,
16-
- ``cbook.is_numlike`` (use ``isinstance(..., numbers.Number)`` instead),
17-
``cbook.listFiles``, ``cbook.unicode_safe``
16+
- ``cbook.get_label``, ``cbook.is_numlike`` (use
17+
``isinstance(..., numbers.Number)`` instead), ``cbook.listFiles``,
18+
``cbook.unicode_safe``
1819
- ``container.Container.set_remove_method``,
1920
- ``dates.DateFormatter.strftime_pre_1900``, ``dates.DateFormatter.strftime``,
2021
- ``font_manager.TempCache``,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Axes methods now raise TypeError instead of RuntimeError on mismatched calls
2+
````````````````````````````````````````````````````````````````````````````
3+
4+
In certain cases, Axes methods (and pyplot functions) used to raise a
5+
RuntimeError if they were called with a ``data`` kwarg and otherwise mismatched
6+
arguments. They now raise a ``TypeError`` instead.

lib/matplotlib/__init__.py

Lines changed: 117 additions & 227 deletions
Large diffs are not rendered by default.

lib/matplotlib/axes/_axes.py

Lines changed: 29 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,6 @@
4444
rcParams = matplotlib.rcParams
4545

4646

47-
def _plot_args_replacer(args, data):
48-
if len(args) == 1:
49-
return ["y"]
50-
elif len(args) == 2:
51-
# this can be two cases: x,y or y,c
52-
if (not args[1] in data and
53-
not (hasattr(data, 'dtype') and
54-
hasattr(data.dtype, 'names') and
55-
data.dtype.names is not None and
56-
args[1] in data.dtype.names)):
57-
# this is not in data, so just assume that it is something which
58-
# will not get replaced (color spec or array like).
59-
return ["y", "c"]
60-
# it's data, but could be a color code like 'ro' or 'b--'
61-
# -> warn the user in that case...
62-
try:
63-
_process_plot_format(args[1])
64-
except ValueError:
65-
pass
66-
else:
67-
warnings.warn(
68-
"Second argument {!r} is ambiguous: could be a color spec but "
69-
"is in data; using as data. Either rename the entry in data "
70-
"or use three arguments to plot.".format(args[1]),
71-
RuntimeWarning, stacklevel=3)
72-
return ["x", "y"]
73-
elif len(args) == 3:
74-
return ["x", "y", "c"]
75-
else:
76-
raise ValueError("Using arbitrary long args with data is not "
77-
"supported due to ambiguity of arguments.\nUse "
78-
"multiple plotting calls instead.")
79-
80-
8147
# The axes module contains all the wrappers to plotting functions.
8248
# All the other methods should go in the _AxesBase class.
8349

@@ -894,8 +860,7 @@ def vlines(self, x, ymin, ymax, colors='k', linestyles='solid',
894860

895861
@_preprocess_data(replace_names=["positions", "lineoffsets",
896862
"linelengths", "linewidths",
897-
"colors", "linestyles"],
898-
label_namer=None)
863+
"colors", "linestyles"])
899864
@docstring.dedent_interpd
900865
def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
901866
linelengths=1, linewidths=None, colors=None,
@@ -1110,10 +1075,8 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
11101075

11111076
#### Basic plotting
11121077

1113-
# The label_naming happens in `matplotlib.axes._base._plot_args`
1114-
@_preprocess_data(replace_names=["x", "y"],
1115-
positional_parameter_names=_plot_args_replacer,
1116-
label_namer=None)
1078+
# Uses a custom implementation of data-kwarg handling in
1079+
# _process_plot_var_args.
11171080
@docstring.dedent_interpd
11181081
def plot(self, *args, **kwargs):
11191082
"""
@@ -1226,7 +1189,6 @@ def plot(self, *args, **kwargs):
12261189
You may suppress the warning by adding an empty format string
12271190
`plot('n', 'o', '', data=obj)`.
12281191
1229-
12301192
Other Parameters
12311193
----------------
12321194
scalex, scaley : bool, optional, default: True
@@ -1253,13 +1215,11 @@ def plot(self, *args, **kwargs):
12531215
lines
12541216
A list of `.Line2D` objects representing the plotted data.
12551217
1256-
12571218
See Also
12581219
--------
12591220
scatter : XY scatter plot with markers of variing size and/or color (
12601221
sometimes also called bubble chart).
12611222
1262-
12631223
Notes
12641224
-----
12651225
**Format Strings**
@@ -1732,7 +1692,7 @@ def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none,
17321692

17331693
#### Specialized plotting
17341694

1735-
@_preprocess_data(replace_names=["x", "y"], label_namer="y")
1695+
# @_preprocess_data() # let 'plot' do the unpacking..
17361696
def step(self, x, y, *args, **kwargs):
17371697
"""
17381698
Make a step plot.
@@ -1803,15 +1763,7 @@ def step(self, x, y, *args, **kwargs):
18031763

18041764
return self.plot(x, y, *args, **kwargs)
18051765

1806-
@_preprocess_data(replace_names=["x", "left",
1807-
"height", "width",
1808-
"y", "bottom",
1809-
"color", "edgecolor", "linewidth",
1810-
"tick_label", "xerr", "yerr",
1811-
"ecolor"],
1812-
label_namer=None,
1813-
replace_all_args=True
1814-
)
1766+
@_preprocess_data()
18151767
@docstring.dedent_interpd
18161768
def bar(self, *args, **kwargs):
18171769
r"""
@@ -2265,7 +2217,7 @@ def barh(self, *args, **kwargs):
22652217
bottom=y, **kwargs)
22662218
return patches
22672219

2268-
@_preprocess_data(label_namer=None)
2220+
@_preprocess_data()
22692221
@docstring.dedent_interpd
22702222
def broken_barh(self, xranges, yrange, **kwargs):
22712223
"""
@@ -2336,7 +2288,7 @@ def broken_barh(self, xranges, yrange, **kwargs):
23362288

23372289
return col
23382290

2339-
@_preprocess_data(replace_all_args=True, label_namer=None)
2291+
@_preprocess_data()
23402292
def stem(self, *args, **kwargs):
23412293
"""
23422294
Create a stem plot.
@@ -2525,8 +2477,7 @@ def stem(self, *args, **kwargs):
25252477

25262478
return stem_container
25272479

2528-
@_preprocess_data(replace_names=["x", "explode", "labels", "colors"],
2529-
label_namer=None)
2480+
@_preprocess_data(replace_names=["x", "explode", "labels", "colors"])
25302481
def pie(self, x, explode=None, labels=None, colors=None,
25312482
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
25322483
startangle=None, radius=None, counterclock=True,
@@ -3133,7 +3084,7 @@ def extract_err(err, data):
31333084

31343085
return errorbar_container # (l0, caplines, barcols)
31353086

3136-
@_preprocess_data(label_namer=None)
3087+
@_preprocess_data()
31373088
def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
31383089
positions=None, widths=None, patch_artist=None,
31393090
bootstrap=None, usermedians=None, conf_intervals=None,
@@ -4612,7 +4563,7 @@ def _quiver_units(self, args, kw):
46124563
return args
46134564

46144565
# args can by a combination if X, Y, U, V, C and all should be replaced
4615-
@_preprocess_data(replace_all_args=True, label_namer=None)
4566+
@_preprocess_data()
46164567
def quiver(self, *args, **kw):
46174568
# Make sure units are handled for x and y values
46184569
args = self._quiver_units(args, kw)
@@ -4625,13 +4576,12 @@ def quiver(self, *args, **kw):
46254576
quiver.__doc__ = mquiver.Quiver.quiver_doc
46264577

46274578
# args can by either Y or y1,y2,... and all should be replaced
4628-
@_preprocess_data(replace_all_args=True, label_namer=None)
4579+
@_preprocess_data()
46294580
def stackplot(self, x, *args, **kwargs):
46304581
return mstack.stackplot(self, x, *args, **kwargs)
46314582
stackplot.__doc__ = mstack.stackplot.__doc__
46324583

4633-
@_preprocess_data(replace_names=["x", "y", "u", "v", "start_points"],
4634-
label_namer=None)
4584+
@_preprocess_data(replace_names=["x", "y", "u", "v", "start_points"])
46354585
def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
46364586
cmap=None, norm=None, arrowsize=1, arrowstyle='-|>',
46374587
minlength=0.1, transform=None, zorder=None,
@@ -4656,7 +4606,7 @@ def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
46564606
streamplot.__doc__ = mstream.streamplot.__doc__
46574607

46584608
# args can be some combination of X, Y, U, V, C and all should be replaced
4659-
@_preprocess_data(replace_all_args=True, label_namer=None)
4609+
@_preprocess_data()
46604610
@docstring.dedent_interpd
46614611
def barbs(self, *args, **kw):
46624612
"""
@@ -4670,8 +4620,8 @@ def barbs(self, *args, **kw):
46704620
self.autoscale_view()
46714621
return b
46724622

4673-
@_preprocess_data(replace_names=["x", "y"], label_namer=None,
4674-
positional_parameter_names=["x", "y", "c"])
4623+
# Uses a custom implementation of data-kwarg handling in
4624+
# _process_plot_var_args.
46754625
def fill(self, *args, **kwargs):
46764626
"""
46774627
Plot filled polygons.
@@ -4718,8 +4668,7 @@ def fill(self, *args, **kwargs):
47184668
self.autoscale_view()
47194669
return patches
47204670

4721-
@_preprocess_data(replace_names=["x", "y1", "y2", "where"],
4722-
label_namer=None)
4671+
@_preprocess_data(replace_names=["x", "y1", "y2", "where"])
47234672
@docstring.dedent_interpd
47244673
def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
47254674
step=None, **kwargs):
@@ -4901,8 +4850,7 @@ def get_interp_point(ind):
49014850
self.autoscale_view()
49024851
return collection
49034852

4904-
@_preprocess_data(replace_names=["y", "x1", "x2", "where"],
4905-
label_namer=None)
4853+
@_preprocess_data(replace_names=["y", "x1", "x2", "where"])
49064854
@docstring.dedent_interpd
49074855
def fill_betweenx(self, y, x1, x2=0, where=None,
49084856
step=None, interpolate=False, **kwargs):
@@ -5084,7 +5032,7 @@ def get_interp_point(ind):
50845032
return collection
50855033

50865034
#### plotting z(x,y): imshow, pcolor and relatives, contour
5087-
@_preprocess_data(label_namer=None)
5035+
@_preprocess_data()
50885036
def imshow(self, X, cmap=None, norm=None, aspect=None,
50895037
interpolation=None, alpha=None, vmin=None, vmax=None,
50905038
origin=None, extent=None, shape=None, filternorm=1,
@@ -5313,7 +5261,7 @@ def _pcolorargs(funcname, *args, **kw):
53135261
C = cbook.safe_masked_invalid(C)
53145262
return X, Y, C
53155263

5316-
@_preprocess_data(label_namer=None)
5264+
@_preprocess_data()
53175265
@docstring.dedent_interpd
53185266
def pcolor(self, *args, **kwargs):
53195267
"""
@@ -5559,7 +5507,7 @@ def pcolor(self, *args, **kwargs):
55595507
self.autoscale_view()
55605508
return collection
55615509

5562-
@_preprocess_data(label_namer=None)
5510+
@_preprocess_data()
55635511
@docstring.dedent_interpd
55645512
def pcolormesh(self, *args, **kwargs):
55655513
"""
@@ -5696,7 +5644,7 @@ def pcolormesh(self, *args, **kwargs):
56965644
self.autoscale_view()
56975645
return collection
56985646

5699-
@_preprocess_data(label_namer=None)
5647+
@_preprocess_data()
57005648
@docstring.dedent_interpd
57015649
def pcolorfast(self, *args, **kwargs):
57025650
"""
@@ -6441,7 +6389,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
64416389
else:
64426390
return tops, bins, cbook.silent_list('Lists of Patches', patches)
64436391

6444-
@_preprocess_data(replace_names=["x", "y", "weights"], label_namer=None)
6392+
@_preprocess_data(replace_names=["x", "y", "weights"])
64456393
def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None,
64466394
cmin=None, cmax=None, **kwargs):
64476395
"""
@@ -6548,7 +6496,7 @@ def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None,
65486496

65496497
return h, xedges, yedges, pc
65506498

6551-
@_preprocess_data(replace_names=["x"], label_namer=None)
6499+
@_preprocess_data(replace_names=["x"])
65526500
@docstring.dedent_interpd
65536501
def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
65546502
window=None, noverlap=None, pad_to=None,
@@ -6783,7 +6731,7 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None,
67836731
else:
67846732
return pxy, freqs, line
67856733

6786-
@_preprocess_data(replace_names=["x"], label_namer=None)
6734+
@_preprocess_data(replace_names=["x"])
67876735
@docstring.dedent_interpd
67886736
def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,
67896737
pad_to=None, sides=None, scale=None,
@@ -6886,7 +6834,7 @@ def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,
68866834

68876835
return spec, freqs, lines[0]
68886836

6889-
@_preprocess_data(replace_names=["x"], label_namer=None)
6837+
@_preprocess_data(replace_names=["x"])
68906838
@docstring.dedent_interpd
68916839
def angle_spectrum(self, x, Fs=None, Fc=None, window=None,
68926840
pad_to=None, sides=None, **kwargs):
@@ -6968,7 +6916,7 @@ def angle_spectrum(self, x, Fs=None, Fc=None, window=None,
69686916

69696917
return spec, freqs, lines[0]
69706918

6971-
@_preprocess_data(replace_names=["x"], label_namer=None)
6919+
@_preprocess_data(replace_names=["x"])
69726920
@docstring.dedent_interpd
69736921
def phase_spectrum(self, x, Fs=None, Fc=None, window=None,
69746922
pad_to=None, sides=None, **kwargs):
@@ -7049,7 +6997,7 @@ def phase_spectrum(self, x, Fs=None, Fc=None, window=None,
70496997

70506998
return spec, freqs, lines[0]
70516999

7052-
@_preprocess_data(replace_names=["x", "y"], label_namer=None)
7000+
@_preprocess_data(replace_names=["x", "y"])
70537001
@docstring.dedent_interpd
70547002
def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
70557003
window=mlab.window_hanning, noverlap=0, pad_to=None,
@@ -7114,7 +7062,7 @@ def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
71147062

71157063
return cxy, freqs
71167064

7117-
@_preprocess_data(replace_names=["x"], label_namer=None)
7065+
@_preprocess_data(replace_names=["x"])
71187066
@docstring.dedent_interpd
71197067
def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
71207068
window=None, noverlap=None,
@@ -7430,7 +7378,7 @@ def matshow(self, Z, **kwargs):
74307378
integer=True))
74317379
return im
74327380

7433-
@_preprocess_data(replace_names=["dataset"], label_namer=None)
7381+
@_preprocess_data(replace_names=["dataset"])
74347382
def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
74357383
showmeans=False, showextrema=True, showmedians=False,
74367384
points=100, bw_method=None):

0 commit comments

Comments
 (0)