diff --git a/examples/statistics/boxplot_demo.py b/examples/statistics/boxplot_demo.py index 83a566e0a2fa..570e9a2428f7 100644 --- a/examples/statistics/boxplot_demo.py +++ b/examples/statistics/boxplot_demo.py @@ -82,9 +82,8 @@ # properties of the original sample, and a boxplot is one visual tool # to make this assessment -numDists = 5 -randomDists = ['Normal(1,1)', ' Lognormal(1,1)', 'Exp(1)', 'Gumbel(6,4)', - 'Triangular(2,9,11)'] +random_dists = ['Normal(1,1)', ' Lognormal(1,1)', 'Exp(1)', 'Gumbel(6,4)', + 'Triangular(2,9,11)'] N = 500 norm = np.random.normal(1, 1, N) @@ -95,15 +94,14 @@ # Generate some random indices that we'll use to resample the original data # arrays. For code brevity, just use the same random indices for each array -bootstrapIndices = np.random.random_integers(0, N - 1, N) -normBoot = norm[bootstrapIndices] -expoBoot = expo[bootstrapIndices] -gumbBoot = gumb[bootstrapIndices] -lognBoot = logn[bootstrapIndices] -triaBoot = tria[bootstrapIndices] - -data = [norm, normBoot, logn, lognBoot, expo, expoBoot, gumb, gumbBoot, - tria, triaBoot] +bootstrap_indices = np.random.randint(0, N, N) +data = [ + norm, norm[bootstrap_indices], + logn, logn[bootstrap_indices], + expo, expo[bootstrap_indices], + gumb, gumb[bootstrap_indices], + tria, tria[bootstrap_indices], +] fig, ax1 = plt.subplots(figsize=(10, 6)) fig.canvas.set_window_title('A Boxplot Example') @@ -126,21 +124,19 @@ ax1.set_ylabel('Value') # Now fill the boxes with desired colors -boxColors = ['darkkhaki', 'royalblue'] -numBoxes = numDists*2 -medians = list(range(numBoxes)) -for i in range(numBoxes): +box_colors = ['darkkhaki', 'royalblue'] +num_boxes = len(data) +medians = np.empty(num_boxes) +for i in range(num_boxes): box = bp['boxes'][i] boxX = [] boxY = [] for j in range(5): boxX.append(box.get_xdata()[j]) boxY.append(box.get_ydata()[j]) - boxCoords = np.column_stack([boxX, boxY]) + box_coords = np.column_stack([boxX, boxY]) # Alternate between Dark Khaki and Royal Blue - k = i % 2 - boxPolygon = Polygon(boxCoords, facecolor=boxColors[k]) - ax1.add_patch(boxPolygon) + ax1.add_patch(Polygon(box_coords, facecolor=box_colors[i % 2])) # Now draw the median lines back over what we just filled in med = bp['medians'][i] medianX = [] @@ -149,39 +145,40 @@ medianX.append(med.get_xdata()[j]) medianY.append(med.get_ydata()[j]) ax1.plot(medianX, medianY, 'k') - medians[i] = medianY[0] + medians[i] = medianY[0] # Finally, overplot the sample averages, with horizontal alignment # in the center of each box - ax1.plot([np.average(med.get_xdata())], [np.average(data[i])], + ax1.plot(np.average(med.get_xdata()), np.average(data[i]), color='w', marker='*', markeredgecolor='k') # Set the axes ranges and axes labels -ax1.set_xlim(0.5, numBoxes + 0.5) +ax1.set_xlim(0.5, num_boxes + 0.5) top = 40 bottom = -5 ax1.set_ylim(bottom, top) -ax1.set_xticklabels(np.repeat(randomDists, 2), +ax1.set_xticklabels(np.repeat(random_dists, 2), rotation=45, fontsize=8) # Due to the Y-axis scale being different across samples, it can be # hard to compare differences in medians across the samples. Add upper # X-axis tick labels with the sample medians to aid in comparison # (just use two decimal places of precision) -pos = np.arange(numBoxes) + 1 -upperLabels = [str(np.round(s, 2)) for s in medians] +pos = np.arange(num_boxes) + 1 +upper_labels = [str(np.round(s, 2)) for s in medians] weights = ['bold', 'semibold'] -for tick, label in zip(range(numBoxes), ax1.get_xticklabels()): +for tick, label in zip(range(num_boxes), ax1.get_xticklabels()): k = tick % 2 - ax1.text(pos[tick], top - (top*0.05), upperLabels[tick], - horizontalalignment='center', size='x-small', weight=weights[k], - color=boxColors[k]) + ax1.text(pos[tick], .95, upper_labels[tick], + transform=ax1.get_xaxis_transform(), + horizontalalignment='center', size='x-small', + weight=weights[k], color=box_colors[k]) # Finally, add a basic legend -fig.text(0.80, 0.08, str(N) + ' Random Numbers', - backgroundcolor=boxColors[0], color='black', weight='roman', +fig.text(0.80, 0.08, f'{N} Random Numbers', + backgroundcolor=box_colors[0], color='black', weight='roman', size='x-small') fig.text(0.80, 0.045, 'IID Bootstrap Resample', - backgroundcolor=boxColors[1], + backgroundcolor=box_colors[1], color='white', weight='roman', size='x-small') fig.text(0.80, 0.015, '*', color='white', backgroundcolor='silver', weight='roman', size='medium') @@ -213,10 +210,10 @@ def fakeBootStrapper(n): return med, CI inc = 0.1 -e1 = np.random.normal(0, 1, size=(500,)) -e2 = np.random.normal(0, 1, size=(500,)) -e3 = np.random.normal(0, 1 + inc, size=(500,)) -e4 = np.random.normal(0, 1 + 2*inc, size=(500,)) +e1 = np.random.normal(0, 1, size=500) +e2 = np.random.normal(0, 1, size=500) +e3 = np.random.normal(0, 1 + inc, size=500) +e4 = np.random.normal(0, 1 + 2*inc, size=500) treatments = [e1, e2, e3, e4] med1, CI1 = fakeBootStrapper(1)