|
| 1 | +""" |
| 2 | +Demo of the new boxplot functionality |
| 3 | +""" |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import matplotlib.pyplot as plt |
| 7 | + |
| 8 | +# fake data |
| 9 | +np.random.seed(937) |
| 10 | +data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75) |
| 11 | +labels = list('ABCD') |
| 12 | +fs = 10 # fontsize |
| 13 | + |
| 14 | +# demonstrate how to toggle the display of different elements: |
| 15 | +fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6,6)) |
| 16 | +axes[0, 0].boxplot(data, labels=labels) |
| 17 | +axes[0, 0].set_title('Default', fontsize=fs) |
| 18 | + |
| 19 | +axes[0, 1].boxplot(data, labels=labels, showmeans=True) |
| 20 | +axes[0, 1].set_title('showmeans=True', fontsize=fs) |
| 21 | + |
| 22 | +axes[0, 2].boxplot(data, labels=labels, showmeans=True, meanline=True) |
| 23 | +axes[0, 2].set_title('showmeans=True,\nmeanline=True', fontsize=fs) |
| 24 | + |
| 25 | +axes[1, 0].boxplot(data, labels=labels, showbox=False, showcaps=False) |
| 26 | +axes[1, 0].set_title('Tufte Style \n(showbox=False,\nshowcaps=False)', fontsize=fs) |
| 27 | + |
| 28 | +axes[1, 1].boxplot(data, labels=labels, notch=True, bootstrap=10000) |
| 29 | +axes[1, 1].set_title('notch=True,\nbootstrap=10000', fontsize=fs) |
| 30 | + |
| 31 | +axes[1, 2].boxplot(data, labels=labels, showfliers=False) |
| 32 | +axes[1, 2].set_title('showfliers=False', fontsize=fs) |
| 33 | + |
| 34 | +for ax in axes.flatten(): |
| 35 | + ax.set_yscale('log') |
| 36 | + ax.set_yticklabels([]) |
| 37 | + |
| 38 | +fig.subplots_adjust(hspace=0.4) |
| 39 | +plt.show() |
| 40 | + |
| 41 | + |
| 42 | +# demonstrate how to customize the display different elements: |
| 43 | +boxprops = dict(linestyle='--', linewidth=3, color='darkgoldenrod') |
| 44 | +flierprops = dict(marker='o', markerfacecolor='green', markersize=12, |
| 45 | + linestyle='none') |
| 46 | +medianprops = dict(linestyle='-.', linewidth=2.5, color='firebrick') |
| 47 | +meanpointprops = dict(marker='D', markeredgecolor='black', |
| 48 | + markerfacecolor='firebrick') |
| 49 | +meanlineprops = dict(linestyle='--', linewidth=2.5, color='purple') |
| 50 | + |
| 51 | +fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6,6)) |
| 52 | +axes[0, 0].boxplot(data, boxprops=boxprops) |
| 53 | +axes[0, 0].set_title('Custom boxprops', fontsize=fs) |
| 54 | + |
| 55 | +axes[0, 1].boxplot(data, flierprops=flierprops, medianprops=medianprops) |
| 56 | +axes[0, 1].set_title('Custom medianprops\nand flierprops', fontsize=fs) |
| 57 | + |
| 58 | +axes[0, 2].boxplot(data, whis='range') |
| 59 | +axes[0, 2].set_title('whis="range"', fontsize=fs) |
| 60 | + |
| 61 | +axes[1, 0].boxplot(data, meanprops=meanpointprops, meanline=False, |
| 62 | + showmeans=True) |
| 63 | +axes[1, 0].set_title('Custom mean\nas point', fontsize=fs) |
| 64 | + |
| 65 | +axes[1, 1].boxplot(data, meanprops=meanlineprops, meanline=True, showmeans=True) |
| 66 | +axes[1, 1].set_title('Custom mean\nas line', fontsize=fs) |
| 67 | + |
| 68 | +axes[1, 2].boxplot(data, whis=[15, 85]) |
| 69 | +axes[1, 2].set_title('whis=[15, 85]\n#percentiles', fontsize=fs) |
| 70 | + |
| 71 | +for ax in axes.flatten(): |
| 72 | + ax.set_yscale('log') |
| 73 | + ax.set_yticklabels([]) |
| 74 | + |
| 75 | +fig.suptitle("I never said they'd be pretty") |
| 76 | +fig.subplots_adjust(hspace=0.4) |
| 77 | +plt.show() |
0 commit comments