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

Skip to content

Commit 83abf80

Browse files
committed
Deprecate positional use of most arguments of plotting functions
This increases maintainability for developers and disallows bad practice of passing lots of positional arguments for users. If in doubt, I've erred on the side of allowing as much positional arguments as possible as long as the intent of a call is still readable. Note: This was originally motivated by bxp() and boxplot() having many overlapping parameters but differently ordered. While at it, I think it's better to rollout the change to all plotting functions and communicate that in one go rather than having lots of individual change notices over time. Also, the freedom to reorder parameters only sets in after the deprecation. So let's start this now.
1 parent f048468 commit 83abf80

File tree

4 files changed

+73
-58
lines changed

4 files changed

+73
-58
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/statistics/boxplot_color.py

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,34 @@
33
Box plots with custom fill colors
44
=================================
55
6-
This plot illustrates how to create two types of box plots
7-
(rectangular and notched), and how to fill them with custom
8-
colors by accessing the properties of the artists of the
9-
box plots. Additionally, the ``labels`` parameter is used to
10-
provide x-tick labels for each sample.
11-
12-
A good general reference on boxplots and their history can be found
13-
here: http://vita.had.co.nz/papers/boxplots.pdf
6+
To color each box of a box plot individually:
7+
8+
1) use the keyword argument ``patch_artist=True`` to create filled boxes.
9+
2) loop through the created boxes and adapt their color.
1410
"""
1511

1612
import matplotlib.pyplot as plt
1713
import numpy as np
1814

19-
# Random test data
2015
np.random.seed(19680801)
21-
all_data = [np.random.normal(0, std, size=100) for std in range(1, 4)]
22-
labels = ['x1', 'x2', 'x3']
23-
24-
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(9, 4))
25-
26-
# rectangular box plot
27-
bplot1 = ax1.boxplot(all_data,
28-
vert=True, # vertical box alignment
29-
patch_artist=True, # fill with color
30-
labels=labels) # will be used to label x-ticks
31-
ax1.set_title('Rectangular box plot')
32-
33-
# notch shape box plot
34-
bplot2 = ax2.boxplot(all_data,
35-
notch=True, # notch shape
36-
vert=True, # vertical box alignment
37-
patch_artist=True, # fill with color
38-
labels=labels) # will be used to label x-ticks
39-
ax2.set_title('Notched box plot')
16+
fruit_weights = [
17+
np.random.normal(130, 10, size=100),
18+
np.random.normal(125, 20, size=100),
19+
np.random.normal(120, 30, size=100),
20+
]
21+
labels = ['peaches', 'oranges', 'tomatoes']
22+
colors = ['peachpuff', 'orange', 'tomato']
23+
24+
fig, ax = plt.subplots()
25+
ax.set_ylabel('fruit weight (g)')
26+
27+
bplot = ax.boxplot(fruit_weights,
28+
patch_artist=True, # fill with color
29+
labels=labels) # will be used to label x-ticks
4030

4131
# fill with colors
42-
colors = ['pink', 'lightblue', 'lightgreen']
43-
for bplot in (bplot1, bplot2):
44-
for patch, color in zip(bplot['boxes'], colors):
45-
patch.set_facecolor(color)
46-
47-
# adding horizontal grid lines
48-
for ax in [ax1, ax2]:
49-
ax.yaxis.grid(True)
50-
ax.set_xlabel('Three separate samples')
51-
ax.set_ylabel('Observed values')
32+
for patch, color in zip(bplot['boxes'], colors):
33+
patch.set_facecolor(color)
5234

5335
plt.show()
5436

lib/matplotlib/axes/_axes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@ def axvspan(self, xmin, xmax, ymin=0, ymax=1, **kwargs):
10571057
self._request_autoscale_view("x")
10581058
return p
10591059

1060+
@_api.make_keyword_only("3.9", "label")
10601061
@_preprocess_data(replace_names=["y", "xmin", "xmax", "colors"],
10611062
label_namer="y")
10621063
def hlines(self, y, xmin, xmax, colors=None, linestyles='solid',
@@ -1148,6 +1149,7 @@ def hlines(self, y, xmin, xmax, colors=None, linestyles='solid',
11481149
self._request_autoscale_view()
11491150
return lines
11501151

1152+
@_api.make_keyword_only("3.9", "label")
11511153
@_preprocess_data(replace_names=["x", "ymin", "ymax", "colors"],
11521154
label_namer="x")
11531155
def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
@@ -1239,6 +1241,7 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
12391241
self._request_autoscale_view()
12401242
return lines
12411243

1244+
@_api.make_keyword_only("3.9", "orientation")
12421245
@_preprocess_data(replace_names=["positions", "lineoffsets",
12431246
"linelengths", "linewidths",
12441247
"colors", "linestyles"])
@@ -1742,6 +1745,7 @@ def plot(self, *args, scalex=True, scaley=True, data=None, **kwargs):
17421745
self._request_autoscale_view("y")
17431746
return lines
17441747

1748+
@_api.make_keyword_only("3.9", "tz")
17451749
@_preprocess_data(replace_names=["x", "y"], label_namer="y")
17461750
@_docstring.dedent_interpd
17471751
def plot_date(self, x, y, fmt='o', tz=None, xdate=True, ydate=False,
@@ -2045,6 +2049,7 @@ def acorr(self, x, **kwargs):
20452049
"""
20462050
return self.xcorr(x, x, **kwargs)
20472051

2052+
@_api.make_keyword_only("3.9", "normed")
20482053
@_preprocess_data(replace_names=["x", "y"], label_namer="y")
20492054
def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none,
20502055
usevlines=True, maxlags=10, **kwargs):
@@ -3112,6 +3117,7 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
31123117
self.add_container(stem_container)
31133118
return stem_container
31143119

3120+
@_api.make_keyword_only("3.9", "explode")
31153121
@_preprocess_data(replace_names=["x", "explode", "labels", "colors"])
31163122
def pie(self, x, explode=None, labels=None, colors=None,
31173123
autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1,
@@ -3388,6 +3394,7 @@ def _errorevery_to_mask(x, errorevery):
33883394
everymask[errorevery] = True
33893395
return everymask
33903396

3397+
@_api.make_keyword_only("3.9", "ecolor")
33913398
@_preprocess_data(replace_names=["x", "y", "xerr", "yerr"],
33923399
label_namer="y")
33933400
@_docstring.dedent_interpd
@@ -3764,6 +3771,7 @@ def apply_mask(arrays, mask):
37643771

37653772
return errorbar_container # (l0, caplines, barcols)
37663773

3774+
@_api.make_keyword_only("3.9", "notch")
37673775
@_preprocess_data()
37683776
def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
37693777
positions=None, widths=None, patch_artist=None,
@@ -4078,6 +4086,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
40784086
capwidths=capwidths)
40794087
return artists
40804088

4089+
@_api.make_keyword_only("3.9", "widths")
40814090
def bxp(self, bxpstats, positions=None, widths=None, vert=True,
40824091
patch_artist=False, shownotches=False, showmeans=False,
40834092
showcaps=True, showbox=True, showfliers=True,
@@ -4531,6 +4540,7 @@ def invalid_shape_exception(csize, xsize):
45314540
colors = None # use cmap, norm after collection is created
45324541
return c, colors, edgecolors
45334542

4543+
@_api.make_keyword_only("3.9", "marker")
45344544
@_preprocess_data(replace_names=["x", "y", "s", "linewidths",
45354545
"edgecolors", "c", "facecolor",
45364546
"facecolors", "color"],
@@ -4811,6 +4821,7 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
48114821

48124822
return collection
48134823

4824+
@_api.make_keyword_only("3.9", "gridsize")
48144825
@_preprocess_data(replace_names=["x", "y", "C"], label_namer="y")
48154826
@_docstring.dedent_interpd
48164827
def hexbin(self, x, y, C=None, gridsize=100, bins=None,
@@ -6590,6 +6601,7 @@ def clabel(self, CS, levels=None, **kwargs):
65906601

65916602
#### Data analysis
65926603

6604+
@_api.make_keyword_only("3.9", "range")
65936605
@_preprocess_data(replace_names=["x", 'weights'], label_namer="x")
65946606
def hist(self, x, bins=None, range=None, density=False, weights=None,
65956607
cumulative=False, bottom=None, histtype='bar', align='mid',
@@ -7134,6 +7146,7 @@ def stairs(self, values, edges=None, *,
71347146
self._request_autoscale_view()
71357147
return patch
71367148

7149+
@_api.make_keyword_only("3.9", "range")
71377150
@_preprocess_data(replace_names=["x", "y", "weights"])
71387151
@_docstring.dedent_interpd
71397152
def hist2d(self, x, y, bins=10, range=None, density=False, weights=None,
@@ -7343,6 +7356,7 @@ def ecdf(self, x, weights=None, *, complementary=False,
73437356
line.sticky_edges.x[:] = [0, 1]
73447357
return line
73457358

7359+
@_api.make_keyword_only("3.9", "NFFT")
73467360
@_preprocess_data(replace_names=["x"])
73477361
@_docstring.dedent_interpd
73487362
def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
@@ -7454,6 +7468,7 @@ def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
74547468
else:
74557469
return pxx, freqs, line
74567470

7471+
@_api.make_keyword_only("3.9", "NFFT")
74577472
@_preprocess_data(replace_names=["x", "y"], label_namer="y")
74587473
@_docstring.dedent_interpd
74597474
def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None,
@@ -7556,6 +7571,7 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None,
75567571
else:
75577572
return pxy, freqs, line
75587573

7574+
@_api.make_keyword_only("3.9", "Fs")
75597575
@_preprocess_data(replace_names=["x"])
75607576
@_docstring.dedent_interpd
75617577
def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,
@@ -7642,6 +7658,7 @@ def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,
76427658

76437659
return spec, freqs, line
76447660

7661+
@_api.make_keyword_only("3.9", "Fs")
76457662
@_preprocess_data(replace_names=["x"])
76467663
@_docstring.dedent_interpd
76477664
def angle_spectrum(self, x, Fs=None, Fc=None, window=None,
@@ -7711,6 +7728,7 @@ def angle_spectrum(self, x, Fs=None, Fc=None, window=None,
77117728

77127729
return spec, freqs, lines[0]
77137730

7731+
@_api.make_keyword_only("3.9", "Fs")
77147732
@_preprocess_data(replace_names=["x"])
77157733
@_docstring.dedent_interpd
77167734
def phase_spectrum(self, x, Fs=None, Fc=None, window=None,
@@ -7780,6 +7798,7 @@ def phase_spectrum(self, x, Fs=None, Fc=None, window=None,
77807798

77817799
return spec, freqs, lines[0]
77827800

7801+
@_api.make_keyword_only("3.9", "NFFT")
77837802
@_preprocess_data(replace_names=["x", "y"])
77847803
@_docstring.dedent_interpd
77857804
def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
@@ -7844,6 +7863,7 @@ def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
78447863

78457864
return cxy, freqs
78467865

7866+
@_api.make_keyword_only("3.9", "NFFT")
78477867
@_preprocess_data(replace_names=["x"])
78487868
@_docstring.dedent_interpd
78497869
def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
@@ -8000,6 +8020,7 @@ def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
80008020

80018021
return spec, freqs, t, im
80028022

8023+
@_api.make_keyword_only("3.9", "precision")
80038024
@_docstring.dedent_interpd
80048025
def spy(self, Z, precision=0, marker=None, markersize=None,
80058026
aspect='equal', origin="upper", **kwargs):
@@ -8190,6 +8211,7 @@ def matshow(self, Z, **kwargs):
81908211
mticker.MaxNLocator(nbins=9, steps=[1, 2, 5, 10], integer=True))
81918212
return im
81928213

8214+
@_api.make_keyword_only("3.9", "vert")
81938215
@_preprocess_data(replace_names=["dataset"])
81948216
def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
81958217
showmeans=False, showextrema=True, showmedians=False,
@@ -8295,6 +8317,7 @@ def _kde_method(X, coords):
82958317
widths=widths, showmeans=showmeans,
82968318
showextrema=showextrema, showmedians=showmedians)
82978319

8320+
@_api.make_keyword_only("3.9", "vert")
82988321
def violin(self, vpstats, positions=None, vert=True, widths=0.5,
82998322
showmeans=False, showextrema=True, showmedians=False):
83008323
"""

0 commit comments

Comments
 (0)