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

Skip to content

Commit 267623c

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 a1d79d2 commit 267623c

File tree

7 files changed

+225
-427
lines changed

7 files changed

+225
-427
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
@@ -15,8 +15,9 @@ The following classes, methods, functions, and attributes are deprecated:
1515
- ``backend_qt5.error_msg_qt``, ``backend_qt5.exception_handler``,
1616
- ``backend_wx.FigureCanvasWx.macros``,
1717
- ``cbook.GetRealpathAndStat``, ``cbook.Locked``,
18-
- ``cbook.is_numlike`` (use ``isinstance(..., numbers.Number)`` instead),
19-
``cbook.listFiles``, ``cbook.unicode_safe``,
18+
- ``cbook.get_label``, ``cbook.is_numlike`` (use
19+
``isinstance(..., numbers.Number)`` instead), ``cbook.listFiles``,
20+
``cbook.unicode_safe``,
2021
- ``container.Container.set_remove_method``,
2122
- ``contour.ContourLabeler.cl``, ``.cl_xy``, and ``.cl_cvalues``,
2223
- ``dates.DateFormatter.strftime_pre_1900``, ``dates.DateFormatter.strftime``,
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: 116 additions & 224 deletions
Large diffs are not rendered by default.

lib/matplotlib/axes/_axes.py

Lines changed: 31 additions & 83 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,
@@ -1111,10 +1076,8 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
11111076

11121077
#### Basic plotting
11131078

1114-
# The label_naming happens in `matplotlib.axes._base._plot_args`
1115-
@_preprocess_data(replace_names=["x", "y"],
1116-
positional_parameter_names=_plot_args_replacer,
1117-
label_namer=None)
1079+
# Uses a custom implementation of data-kwarg handling in
1080+
# _process_plot_var_args.
11181081
@docstring.dedent_interpd
11191082
def plot(self, *args, scalex=True, scaley=True, **kwargs):
11201083
"""
@@ -1227,7 +1190,6 @@ def plot(self, *args, scalex=True, scaley=True, **kwargs):
12271190
You may suppress the warning by adding an empty format string
12281191
`plot('n', 'o', '', data=obj)`.
12291192
1230-
12311193
Other Parameters
12321194
----------------
12331195
scalex, scaley : bool, optional, default: True
@@ -1254,13 +1216,11 @@ def plot(self, *args, scalex=True, scaley=True, **kwargs):
12541216
lines
12551217
A list of `.Line2D` objects representing the plotted data.
12561218
1257-
12581219
See Also
12591220
--------
12601221
scatter : XY scatter plot with markers of variing size and/or color (
12611222
sometimes also called bubble chart).
12621223
1263-
12641224
Notes
12651225
-----
12661226
**Format Strings**
@@ -1729,7 +1689,7 @@ def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none,
17291689

17301690
#### Specialized plotting
17311691

1732-
@_preprocess_data(replace_names=["x", "y"], label_namer="y")
1692+
# @_preprocess_data() # let 'plot' do the unpacking..
17331693
def step(self, x, y, *args, where='pre', **kwargs):
17341694
"""
17351695
Make a step plot.
@@ -1798,15 +1758,7 @@ def step(self, x, y, *args, where='pre', **kwargs):
17981758

17991759
return self.plot(x, y, *args, **kwargs)
18001760

1801-
@_preprocess_data(replace_names=["x", "left",
1802-
"height", "width",
1803-
"y", "bottom",
1804-
"color", "edgecolor", "linewidth",
1805-
"tick_label", "xerr", "yerr",
1806-
"ecolor"],
1807-
label_namer=None,
1808-
replace_all_args=True
1809-
)
1761+
@_preprocess_data()
18101762
@docstring.dedent_interpd
18111763
def bar(self, x, height, width=0.8, bottom=None, *, align="center",
18121764
**kwargs):
@@ -2198,7 +2150,7 @@ def barh(self, y, width, height=0.8, left=None, *, align="center",
21982150
align=align, **kwargs)
21992151
return patches
22002152

2201-
@_preprocess_data(label_namer=None)
2153+
@_preprocess_data()
22022154
@docstring.dedent_interpd
22032155
def broken_barh(self, xranges, yrange, **kwargs):
22042156
"""
@@ -2269,9 +2221,9 @@ def broken_barh(self, xranges, yrange, **kwargs):
22692221

22702222
return col
22712223

2272-
@_preprocess_data(replace_all_args=True, label_namer=None)
2273-
def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None,
2274-
bottom=0, label=None):
2224+
@_preprocess_data()
2225+
def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
2226+
label=None):
22752227
"""
22762228
Create a stem plot.
22772229
@@ -2427,8 +2379,7 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None,
24272379

24282380
return stem_container
24292381

2430-
@_preprocess_data(replace_names=["x", "explode", "labels", "colors"],
2431-
label_namer=None)
2382+
@_preprocess_data(replace_names=["x", "explode", "labels", "colors"])
24322383
def pie(self, x, explode=None, labels=None, colors=None,
24332384
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
24342385
startangle=None, radius=None, counterclock=True,
@@ -3043,7 +2994,7 @@ def extract_err(err, data):
30432994

30442995
return errorbar_container # (l0, caplines, barcols)
30452996

3046-
@_preprocess_data(label_namer=None)
2997+
@_preprocess_data()
30472998
def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
30482999
positions=None, widths=None, patch_artist=None,
30493000
bootstrap=None, usermedians=None, conf_intervals=None,
@@ -4521,7 +4472,7 @@ def _quiver_units(self, args, kw):
45214472
return args
45224473

45234474
# args can by a combination if X, Y, U, V, C and all should be replaced
4524-
@_preprocess_data(replace_all_args=True, label_namer=None)
4475+
@_preprocess_data()
45254476
def quiver(self, *args, **kw):
45264477
# Make sure units are handled for x and y values
45274478
args = self._quiver_units(args, kw)
@@ -4534,13 +4485,12 @@ def quiver(self, *args, **kw):
45344485
quiver.__doc__ = mquiver.Quiver.quiver_doc
45354486

45364487
# args can by either Y or y1,y2,... and all should be replaced
4537-
@_preprocess_data(replace_all_args=True, label_namer=None)
4488+
@_preprocess_data()
45384489
def stackplot(self, x, *args, **kwargs):
45394490
return mstack.stackplot(self, x, *args, **kwargs)
45404491
stackplot.__doc__ = mstack.stackplot.__doc__
45414492

4542-
@_preprocess_data(replace_names=["x", "y", "u", "v", "start_points"],
4543-
label_namer=None)
4493+
@_preprocess_data(replace_names=["x", "y", "u", "v", "start_points"])
45444494
def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
45454495
cmap=None, norm=None, arrowsize=1, arrowstyle='-|>',
45464496
minlength=0.1, transform=None, zorder=None,
@@ -4565,7 +4515,7 @@ def streamplot(self, x, y, u, v, density=1, linewidth=None, color=None,
45654515
streamplot.__doc__ = mstream.streamplot.__doc__
45664516

45674517
# args can be some combination of X, Y, U, V, C and all should be replaced
4568-
@_preprocess_data(replace_all_args=True, label_namer=None)
4518+
@_preprocess_data()
45694519
@docstring.dedent_interpd
45704520
def barbs(self, *args, **kw):
45714521
"""
@@ -4579,8 +4529,8 @@ def barbs(self, *args, **kw):
45794529
self.autoscale_view()
45804530
return b
45814531

4582-
@_preprocess_data(replace_names=["x", "y"], label_namer=None,
4583-
positional_parameter_names=["x", "y", "c"])
4532+
# Uses a custom implementation of data-kwarg handling in
4533+
# _process_plot_var_args.
45844534
def fill(self, *args, **kwargs):
45854535
"""
45864536
Plot filled polygons.
@@ -4627,8 +4577,7 @@ def fill(self, *args, **kwargs):
46274577
self.autoscale_view()
46284578
return patches
46294579

4630-
@_preprocess_data(replace_names=["x", "y1", "y2", "where"],
4631-
label_namer=None)
4580+
@_preprocess_data(replace_names=["x", "y1", "y2", "where"])
46324581
@docstring.dedent_interpd
46334582
def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
46344583
step=None, **kwargs):
@@ -4810,8 +4759,7 @@ def get_interp_point(ind):
48104759
self.autoscale_view()
48114760
return collection
48124761

4813-
@_preprocess_data(replace_names=["y", "x1", "x2", "where"],
4814-
label_namer=None)
4762+
@_preprocess_data(replace_names=["y", "x1", "x2", "where"])
48154763
@docstring.dedent_interpd
48164764
def fill_betweenx(self, y, x1, x2=0, where=None,
48174765
step=None, interpolate=False, **kwargs):
@@ -4993,7 +4941,7 @@ def get_interp_point(ind):
49934941
return collection
49944942

49954943
#### plotting z(x,y): imshow, pcolor and relatives, contour
4996-
@_preprocess_data(label_namer=None)
4944+
@_preprocess_data()
49974945
def imshow(self, X, cmap=None, norm=None, aspect=None,
49984946
interpolation=None, alpha=None, vmin=None, vmax=None,
49994947
origin=None, extent=None, shape=None, filternorm=1,
@@ -5258,7 +5206,7 @@ def _pcolorargs(funcname, *args, allmatch=False):
52585206
C = cbook.safe_masked_invalid(C)
52595207
return X, Y, C
52605208

5261-
@_preprocess_data(label_namer=None)
5209+
@_preprocess_data()
52625210
@docstring.dedent_interpd
52635211
def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
52645212
vmax=None, **kwargs):
@@ -5499,7 +5447,7 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
54995447
self.autoscale_view()
55005448
return collection
55015449

5502-
@_preprocess_data(label_namer=None)
5450+
@_preprocess_data()
55035451
@docstring.dedent_interpd
55045452
def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
55055453
vmax=None, shading='flat', antialiased=False, **kwargs):
@@ -5631,7 +5579,7 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
56315579
self.autoscale_view()
56325580
return collection
56335581

5634-
@_preprocess_data(label_namer=None)
5582+
@_preprocess_data()
56355583
@docstring.dedent_interpd
56365584
def pcolorfast(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
56375585
vmax=None, **kwargs):
@@ -6372,7 +6320,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
63726320
else:
63736321
return tops, bins, cbook.silent_list('Lists of Patches', patches)
63746322

6375-
@_preprocess_data(replace_names=["x", "y", "weights"], label_namer=None)
6323+
@_preprocess_data(replace_names=["x", "y", "weights"])
63766324
def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None,
63776325
cmin=None, cmax=None, **kwargs):
63786326
"""
@@ -6480,7 +6428,7 @@ def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None,
64806428

64816429
return h, xedges, yedges, pc
64826430

6483-
@_preprocess_data(replace_names=["x"], label_namer=None)
6431+
@_preprocess_data(replace_names=["x"])
64846432
@docstring.dedent_interpd
64856433
def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
64866434
window=None, noverlap=None, pad_to=None,
@@ -6715,7 +6663,7 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None,
67156663
else:
67166664
return pxy, freqs, line
67176665

6718-
@_preprocess_data(replace_names=["x"], label_namer=None)
6666+
@_preprocess_data(replace_names=["x"])
67196667
@docstring.dedent_interpd
67206668
def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,
67216669
pad_to=None, sides=None, scale=None,
@@ -6818,7 +6766,7 @@ def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,
68186766

68196767
return spec, freqs, lines[0]
68206768

6821-
@_preprocess_data(replace_names=["x"], label_namer=None)
6769+
@_preprocess_data(replace_names=["x"])
68226770
@docstring.dedent_interpd
68236771
def angle_spectrum(self, x, Fs=None, Fc=None, window=None,
68246772
pad_to=None, sides=None, **kwargs):
@@ -6900,7 +6848,7 @@ def angle_spectrum(self, x, Fs=None, Fc=None, window=None,
69006848

69016849
return spec, freqs, lines[0]
69026850

6903-
@_preprocess_data(replace_names=["x"], label_namer=None)
6851+
@_preprocess_data(replace_names=["x"])
69046852
@docstring.dedent_interpd
69056853
def phase_spectrum(self, x, Fs=None, Fc=None, window=None,
69066854
pad_to=None, sides=None, **kwargs):
@@ -6981,7 +6929,7 @@ def phase_spectrum(self, x, Fs=None, Fc=None, window=None,
69816929

69826930
return spec, freqs, lines[0]
69836931

6984-
@_preprocess_data(replace_names=["x", "y"], label_namer=None)
6932+
@_preprocess_data(replace_names=["x", "y"])
69856933
@docstring.dedent_interpd
69866934
def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
69876935
window=mlab.window_hanning, noverlap=0, pad_to=None,
@@ -7046,7 +6994,7 @@ def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
70466994

70476995
return cxy, freqs
70486996

7049-
@_preprocess_data(replace_names=["x"], label_namer=None)
6997+
@_preprocess_data(replace_names=["x"])
70506998
@docstring.dedent_interpd
70516999
def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
70527000
window=None, noverlap=None,
@@ -7371,7 +7319,7 @@ def matshow(self, Z, **kwargs):
73717319
integer=True))
73727320
return im
73737321

7374-
@_preprocess_data(replace_names=["dataset"], label_namer=None)
7322+
@_preprocess_data(replace_names=["dataset"])
73757323
def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
73767324
showmeans=False, showextrema=True, showmedians=False,
73777325
points=100, bw_method=None):

0 commit comments

Comments
 (0)