8000 Merge pull request #13088 from anntzer/deprecated-random-integers · matplotlib/matplotlib@354a430 · GitHub
[go: up one dir, main page]

Skip to content

Commit 354a430

Browse files
authored
Merge pull request #13088 from anntzer/deprecated-random-integers
Don't use deprecated np.random.random_integers.
2 parents 57e1eac + 307091e commit 354a430

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

examples/statistics/boxplot_demo.py

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@
8282
# properties of the original sample, and a boxplot is one visual tool
8383
# to make this assessment
8484

85-
numDists = 5
86-
randomDists = ['Normal(1,1)', ' Lognormal(1,1)', 'Exp(1)', 'Gumbel(6,4)',
87-
'Triangular(2,9,11)']
85+
random_dists = ['Normal(1,1)', ' Lognormal(1,1)', 'Exp(1)', 'Gumbel(6,4)',
86+
'Triangular(2,9,11)']
8887
N = 500
8988

9089
norm = np.random.normal(1, 1, N)
@@ -95,15 +94,14 @@
9594

9695
# Generate some random indices that we'll use to resample the original data
9796
# arrays. For code brevity, just use the same random indices for each array
98-
bootstrapIndices = np.random.random_integers(0, N - 1, N)
99-
normBoot = norm[bootstrapIndices]
100-
expoBoot = expo[bootstrapIndices]
101-
gumbBoot = gumb[bootstrapIndices]
102-
lognBoot = logn[bootstrapIndices]
103-
triaBoot = tria[bootstrapIndices]
104-
105-
data = [norm, normBoot, logn, lognBoot, expo, expoBoot, gumb, gumbBoot,
106-
tria, triaBoot]
97+
bootstrap_indices = np.random.randint(0, N, N)
98+
data = [
99+
norm, norm[bootstrap_indices],
100+
logn, logn[bootstrap_indices],
101+
expo, expo[bootstrap_indices],
102+
gumb, gumb[bootstrap_indices],
103+
tria, tria[bootstrap_indices],
104+
]
107105

108106
fig, ax1 = plt.subplots(figsize=(10, 6))
109107
fig.canvas.set_window_title('A Boxplot Example')
@@ -126,21 +124,19 @@
126124
ax1.set_ylabel('Value')
127125

128126
# Now fill the boxes with desired colors
129-
boxColors = ['darkkhaki', 'royalblue']
130-
numBoxes = numDists*2
131-
medians = list(range(numBoxes))
132-
for i in range(numBoxes):
127+
box_colors = ['darkkhaki', 'royalblue']
128+
num_boxes = len(data)
129+
medians = np.empty(num_boxes)
130+
for i in range(num_boxes):
133131
box = bp['boxes'][i]
134132
boxX = []
135133
boxY = []
136134
for j in range(5):
137135
boxX.append(box.get_xdata()[j])
138136
boxY.append(box.get_ydata()[j])
139-
boxCoords = np.column_stack([boxX, boxY])
137+
box_coords = np.column_stack([boxX, boxY])
140138
# Alternate between Dark Khaki and Royal Blue
141-
k = i % 2
142-
boxPolygon = Polygon(boxCoords, facecolor=boxColors[k])
143-
ax1.add_patch(boxPolygon)
139+
ax1.add_patch(Polygon(box_coords, facecolor=box_colors[i % 2]))
144140
# Now draw the median lines back over what we just filled in
145141
med = bp['medians'][i]
146142
medianX = []
@@ -149,39 +145,40 @@
149145
medianX.append(med.get_xdata()[j])
150146
medianY.append(med.get_ydata()[j])
151147
ax1.plot(medianX, medianY, 'k')
152-
medians[i] = medianY[0]
148+
medians[i] = medianY[0]
153149
# Finally, overplot the sample averages, with horizontal alignment
154150
# in the center of each box
155-
ax1.plot([np.average(med.get_xdata())], [np.average(data[i])],
151+
ax1.plot(np.average(med.get_xdata()), np.average(data[i]),
156152
color='w', marker='*', markeredgecolor='k')
157153

158154
# Set the axes ranges and axes labels
159-
ax1.set_xlim(0.5, numBoxes + 0.5)
155+
ax1.set_xlim(0.5, num_boxes + 0.5)
160156
top = 40
161157
bottom = -5
162158
ax1.set_ylim(bottom, top)
163-
ax1.set_xticklabels(np.repeat(randomDists, 2),
159+
ax1.set_xticklabels(np.repeat(random_dists, 2),
164160
rotation=45, fontsize=8)
165161

166162
# Due to the Y-axis scale being different across samples, it can be
167163
# hard to compare differences in medians across the samples. Add upper
168164
# X-axis tick labels with the sample medians to aid in comparison
169165
# (just use two decimal places of precision)
170-
pos = np.arange(numBoxes) + 1
171-
upperLabels = [str(np.round(s, 2)) for s in medians]
166+
pos = np.arange(num_boxes) + 1
167+
upper_labels = [str(np.round(s, 2)) for s in medians]
172168
weights = ['bold', 'semibold']
173-
for tick, label in zip(range(numBoxes), ax1.get_xticklabels()):
169+
for tick, label in zip(range(num_boxes), ax1.get_xticklabels()):
174170
k = tick % 2
175-
ax1.text(pos[tick], top - (top*0.05), upperLabels[tick],
176-
horizontalalignment='center', size='x-small', weight=weights[k],
177-
color=boxColors[k])
171+
ax1.text(pos[tick], .95, upper_labels[tick],
172+
transform=ax1.get_xaxis_transform(),
173+
horizontalalignment='center', size='x-small',
174+
weight=weights[k], color=box_colors[k])
178175

179176
# Finally, add a basic legend
180-
fig.text(0.80, 0.08, str(N) + ' Random Numbers',
181-
backgroundcolor=boxColors[0], color='black', weight='roman',
177+
fig.text(0.80, 0.08, f'{N} Random Numbers',
178+
backgroundcolor=box_colors[0], color='black', weight='roman',
182179
size='x-small')
183180
fig.text(0.80, 0.045, 'IID Bootstrap Resample',
184-
backgroundcolor=boxColors[1],
181+
backgroundcolor=box_colors[1],
185182
color='white', weight='roman', size='x-small')
186183
fig.text(0.80, 0.015, '*', color='white', backgroundcolor='silver',
187184
weight='roman', size='medium')
@@ -213,10 +210,10 @@ def fakeBootStrapper(n):
213210
return med, CI
214211

215212
inc = 0.1
216-
e1 = np.random.normal(0, 1, size=(500,))
217-
e2 = np.random.normal(0, 1, size=(500,))
218-
e3 = np.random.normal(0, 1 + inc, size=(500,))
219-
e4 = np.random.normal(0, 1 + 2*inc, size=(500,))
213+
e1 = np.random.normal(0, 1, size=500)
214+
e2 = np.random.normal(0, 1, size=500)
215+
e3 = np.random.normal(0, 1 + inc, size=500)
216+
e4 = np.random.normal(0, 1 + 2*inc, size=500)
220217

221218
treatments = [e1, e2, e3, e4]
222219
med1, CI1 = fakeBootStrapper(1)

0 commit comments

Comments
 (0)
0