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

Skip to content

Commit 5e0aa54

Browse files
authored
Merge pull request matplotlib#27786 from timhoffm/kwonly
Deprecate positional use of most arguments of plotting functions
2 parents b3ab4a3 + 163758a commit 5e0aa54

File tree

8 files changed

+61
-28
lines changed

8 files changed

+61
-28
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Positional parameters in plotting functions
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Many plotting functions will restrict positional arguments to the first few parameters
5+
in the future. All further configuration parameters will have to be passed as keyword
6+
arguments. This is to enforce better code and and allow for future changes with reduced
7+
risk of breaking existing code.

galleries/examples/lines_bars_and_markers/cohere.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
axs[0].set_ylabel('s1 and s2')
2828
axs[0].grid(True)
2929

30-
cxy, f = axs[1].cohere(s1, s2, 256, 1. / dt)
30+
cxy, f = axs[1].cohere(s1, s2, NFFT=256, Fs=1. / dt)
3131
axs[1].set_ylabel('Coherence')
3232

3333
plt.show()

galleries/examples/lines_bars_and_markers/csd_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
ax1.set_ylabel('s1 and s2')
3535
ax1.grid(True)
3636

37-
cxy, f = ax2.csd(s1, s2, 256, 1. / dt)
37+
cxy, f = ax2.csd(s1, s2, NFFT=256, Fs=1. / dt)
3838
ax2.set_ylabel('CSD (dB)')
3939

4040
plt.show()

galleries/examples/lines_bars_and_markers/psd_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
ax0.plot(t, s)
3131
ax0.set_xlabel('Time (s)')
3232
ax0.set_ylabel('Signal')
33-
ax1.psd(s, 512, 1 / dt)
33+
ax1.psd(s, NFFT=512, Fs=1 / dt)
3434

3535
plt.show()
3636

galleries/examples/statistics/boxplot_demo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@
3434
axs[0, 0].set_title('basic plot')
3535

3636
# notched plot
37-
axs[0, 1].boxplot(data, 1)
37+
axs[0, 1].boxplot(data, notch=True)
3838
axs[0, 1].set_title('notched plot')
3939

4040
# change outlier point symbols
41-
axs[0, 2].boxplot(data, 0, 'gD')
41+
axs[0, 2].boxplot(data, sym='gD')
4242
axs[0, 2].set_title('change outlier\npoint symbols')
4343

4444
# don't show outlier points
45-
axs[1, 0].boxplot(data, 0, '')
45+
axs[1, 0].boxplot(data, sym='')
4646
axs[1, 0].set_title("don't show\noutlier points")
4747

4848
# horizontal boxes
49-
axs[1, 1].boxplot(data, 0, 'rs', 0)
49+
axs[1, 1].boxplot(data, sym='rs', vert=False)
5050
axs[1, 1].set_title('horizontal boxes')
5151

5252
# change whisker length
53-
axs[1, 2].boxplot(data, 0, 'rs', 0, 0.75)
53+
axs[1, 2].boxplot(data, sym='rs', vert=False, whis=0.75)
5454
axs[1, 2].set_title('change whisker length')
5555

5656
fig.subplots_adjust(left=0.08, right=0.98, bottom=0.05, top=0.9,

lib/matplotlib/_api/deprecation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ def make_keyword_only(since, name, func=None):
437437
assert (name in signature.parameters
438438
and signature.parameters[name].kind == POK), (
439439
f"Matplotlib internal error: {name!r} must be a positional-or-keyword "
440-
f"parameter for {func.__name__}()")
440+
f"parameter for {func.__name__}(). If this error happens on a function with a "
441+
f"pyplot wrapper, make sure make_keyword_only() is the outermost decorator.")
441442
names = [*signature.parameters]
442443
name_idx = names.index(name)
443444
kwonly = [name for name in names[name_idx:]

lib/matplotlib/axes/_axes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
11001100
self._request_autoscale_view("x")
11011101
return p
11021102

1103+
@_api.make_keyword_only("3.9", "label")
11031104
@_preprocess_data(replace_names=["y", "xmin", "xmax", "colors"],
11041105
label_namer="y")
11051106
def hlines(self, y, xmin, xmax, colors=None, linestyles='solid',
@@ -1191,6 +1192,7 @@ def hlines(self, y, xmin, xmax, colors=None, linestyles='solid',
11911192
self._request_autoscale_view()
11921193
return lines
11931194

1195+
@_api.make_keyword_only("3.9", "label")
11941196
@_preprocess_data(replace_names=["x", "ymin", "ymax", "colors"],
11951197
label_namer="x")
11961198
def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
@@ -1282,6 +1284,7 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
12821284
self._request_autoscale_view()
12831285
return lines
12841286

1287+
@_api.make_keyword_only("3.9", "orientation")
12851288
@_preprocess_data(replace_names=["positions", "lineoffsets",
12861289
"linelengths", "linewidths",
12871290
"colors", "linestyles"])
@@ -2088,6 +2091,7 @@ def acorr(self, x, **kwargs):
20882091
"""
20892092
return self.xcorr(x, x, **kwargs)
20902093

2094+
@_api.make_keyword_only("3.9", "normed")
20912095
@_preprocess_data(replace_names=["x", "y"], label_namer="y")
20922096
def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none,
20932097
usevlines=True, maxlags=10, **kwargs):
@@ -3155,6 +3159,7 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
31553159
self.add_container(stem_container)
31563160
return stem_container
31573161

3162+
@_api.make_keyword_only("3.9", "explode")
31583163
@_preprocess_data(replace_names=["x", "explode", "labels", "colors"])
31593164
def pie(self, x, explode=None, labels=None, colors=None,
31603165
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
@@ -3434,6 +3439,7 @@ def _errorevery_to_mask(x, errorevery):
34343439
everymask[errorevery] = True
34353440
return everymask
34363441

3442+
@_api.make_keyword_only("3.9", "ecolor")
34373443
@_preprocess_data(replace_names=["x", "y", "xerr", "yerr"],
34383444
label_namer="y")
34393445
@_docstring.dedent_interpd
@@ -3810,6 +3816,7 @@ def apply_mask(arrays, mask):
38103816

38113817
return errorbar_container # (l0, caplines, barcols)
38123818

3819+
@_api.make_keyword_only("3.9", "notch")
38133820
@_preprocess_data()
38143821
@_api.rename_parameter("3.9", "labels", "tick_labels")
38153822
def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
@@ -4144,6 +4151,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
41444151
capwidths=capwidths, label=label)
41454152
return artists
41464153

4154+
@_api.make_keyword_only("3.9", "widths")
41474155
def bxp(self, bxpstats, positions=None, widths=None, vert=True,
41484156
patch_artist=False, shownotches=False, showmeans=False,
41494157
showcaps=True, showbox=True, showfliers=True,
@@ -4636,6 +4644,7 @@ def invalid_shape_exception(csize, xsize):
46364644
colors = None # use cmap, norm after collection is created
46374645
return c, colors, edgecolors
46384646

4647+
@_api.make_keyword_only("3.9", "marker")
46394648
@_preprocess_data(replace_names=["x", "y", "s", "linewidths",
46404649
"edgecolors", "c", "facecolor",
46414650
"facecolors", "color"],
@@ -4916,6 +4925,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
49164925

49174926
return collection
49184927

4928+
@_api.make_keyword_only("3.9", "gridsize")
49194929
@_preprocess_data(replace_names=["x", "y", "C"], label_namer="y")
49204930
@_docstring.dedent_interpd
49214931
def hexbin(self, x, y, C=None, gridsize=100, bins=None,
@@ -6698,6 +6708,7 @@ def clabel(self, CS, levels=None, **kwargs):
66986708

66996709
#### Data analysis
67006710

6711+
@_api.make_keyword_only("3.9", "range")
67016712
@_preprocess_data(replace_names=["x", 'weights'], label_namer="x")
67026713
def hist(self, x, bins=None, range=None, density=False, weights=None,
67036714
cumulative=False, bottom=None, histtype='bar', align='mid',
@@ -7245,6 +7256,7 @@ def stairs(self, values, edges=None, *,
72457256
self._request_autoscale_view()
72467257
return patch
72477258

7259+
@_api.make_keyword_only("3.9", "range")
72487260
@_preprocess_data(replace_names=["x", "y", "weights"])
72497261
@_docstring.dedent_interpd
72507262
def hist2d(self, x, y, bins=10, range=None, density=False, weights=None,
@@ -7454,6 +7466,7 @@ def ecdf(self, x, weights=None, *, complementary=False,
74547466
line.sticky_edges.x[:] = [0, 1]
74557467
return line
74567468

7469+
@_api.make_keyword_only("3.9", "NFFT")
74577470
@_preprocess_data(replace_names=["x"])
74587471
@_docstring.dedent_interpd
74597472
def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
@@ -7565,6 +7578,7 @@ def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
75657578
else:
75667579
return pxx, freqs, line
75677580

7581+
@_api.make_keyword_only("3.9", "NFFT")
75687582
@_preprocess_data(replace_names=["x", "y"], label_namer="y")
75697583
@_docstring.dedent_interpd
75707584
def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None,
@@ -7667,6 +7681,7 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None,
76677681
else:
76687682
return pxy, freqs, line
76697683

7684+
@_api.make_keyword_only("3.9", "Fs")
76707685
@_preprocess_data(replace_names=["x"])
76717686
@_docstring.dedent_interpd
76727687
def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,
@@ -7753,6 +7768,7 @@ def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,
77537768

77547769
return spec, freqs, line
77557770

7771+
@_api.make_keyword_only("3.9", "Fs")
77567772
@_preprocess_data(replace_names=["x"])
77577773
@_docstring.dedent_interpd
77587774
def angle_spectrum(self, x, Fs=None, Fc=None, window=None,
@@ -7822,6 +7838,7 @@ def angle_spectrum(self, x, Fs=None, Fc=None, window=None,
78227838

78237839
return spec, freqs, lines[0]
78247840

7841+
@_api.make_keyword_only("3.9", "Fs")
78257842
@_preprocess_data(replace_names=["x"])
78267843
@_docstring.dedent_interpd
78277844
def phase_spectrum(self, x, Fs=None, Fc=None, window=None,
@@ -7891,6 +7908,7 @@ def phase_spectrum(self, x, Fs=None, Fc=None, window=None,
78917908

78927909
return spec, freqs, lines[0]
78937910

7911+
@_api.make_keyword_only("3.9", "NFFT")
78947912
@_preprocess_data(replace_names=["x", "y"])
78957913
@_docstring.dedent_interpd
78967914
def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
@@ -7955,6 +7973,7 @@ def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
79557973

79567974
return cxy, freqs
79577975

7976+
@_api.make_keyword_only("3.9", "NFFT")
79587977
@_preprocess_data(replace_names=["x"])
79597978
@_docstring.dedent_interpd
79607979
def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
@@ -8111,6 +8130,7 @@ def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
81118130

81128131
return spec, freqs, t, im
81138132

8133+
@_api.make_keyword_only("3.9", "precision")
81148134
@_docstring.dedent_interpd
81158135
def spy(self, Z, precision=0, marker=None, markersize=None,
81168136
aspect='equal', origin="upper", **kwargs):
@@ -8301,6 +8321,7 @@ def matshow(self, Z, **kwargs):
83018321
mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True))
83028322
return im
83038323

8324+
@_api.make_keyword_only("3.9", "vert")
83048325
@_preprocess_data(replace_names=["dataset"])
83058326
def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
83068327
showmeans=False, showextrema=True, showmedians=False,
@@ -8412,6 +8433,7 @@ def _kde_method(X, coords):
84128433
widths=widths, showmeans=showmeans,
84138434
showextrema=showextrema, showmedians=showmedians, side=side)
84148435

8436+
@_api.make_keyword_only("3.9", "vert")
84158437
def violin(self, vpstats, positions=None, vert=True, widths=0.5,
84168438
showmeans=False, showextrema=True, showmedians=False, side='both'):
84178439
"""

0 commit comments

Comments
 (0)