|
4 | 4 | # Example boxplot code |
5 | 5 | # |
6 | 6 |
|
7 | | -from pylab import * |
| 7 | +import numpy as np |
| 8 | +import matplotlib.pyplot as plt |
8 | 9 |
|
9 | | -# fake up some data |
10 | | -spread= rand(50) * 100 |
11 | | -center = ones(25) * 50 |
12 | | -flier_high = rand(10) * 100 + 100 |
13 | | -flier_low = rand(10) * -100 |
14 | | -data =concatenate((spread, center, flier_high, flier_low), 0) |
15 | | - |
16 | | -# basic plot |
17 | | -boxplot(data) |
18 | | -#savefig('box1') |
19 | | - |
20 | | -# notched plot |
21 | | -figure() |
22 | | -boxplot(data,1) |
23 | | -#savefig('box2') |
24 | | - |
25 | | -# change outlier point symbols |
26 | | -figure() |
27 | | -boxplot(data,0,'gD') |
28 | | -#savefig('box3') |
29 | | - |
30 | | -# don't show outlier points |
31 | | -figure() |
32 | | -boxplot(data,0,'') |
33 | | -#savefig('box4') |
| 10 | +np.random.seed(0) |
34 | 11 |
|
35 | | -# horizontal boxes |
36 | | -figure() |
37 | | -boxplot(data,0,'rs',0) |
38 | | -#savefig('box5') |
39 | | - |
40 | | -# change whisker length |
41 | | -figure() |
42 | | -boxplot(data,0,'rs',0,0.75) |
43 | | -#savefig('box6') |
| 12 | +# fake up some data |
| 13 | +spread = np.random.rand(50) * 100 |
| 14 | +center = np.ones(25) * 50 |
| 15 | +flier_high = np.random.rand(10) * 100 + 100 |
| 16 | +flier_low = np.random.rand(10) * -100 |
| 17 | +data = np.concatenate((spread, center, flier_high, flier_low), 0) |
| 18 | + |
| 19 | +fig1, ax1 = plt.subplots() |
| 20 | +ax1.set_title('Basic Plot') |
| 21 | +ax1.boxplot(data) |
| 22 | + |
| 23 | +fig2, ax2 = plt.subplots() |
| 24 | +ax2.set_title('Notched boxes') |
| 25 | +ax2.boxplot(data, notch=True) |
| 26 | + |
| 27 | +green_diamond = dict(markerfacecolor='g', marker='D') |
| 28 | +fig3, ax3 = plt.subplots() |
| 29 | +ax3.set_title('Changed Outlier Symbols') |
| 30 | +ax3.boxplot(data, flierprops=green_diamond) |
| 31 | + |
| 32 | +fig4, ax4 = plt.subplots() |
| 33 | +ax4.set_title('Hide Outlier Points') |
| 34 | +ax4.boxplot(data, showfliers=False) |
| 35 | + |
| 36 | +red_square = dict(markerfacecolor='r', marker='s') |
| 37 | +fig5, ax5 = plt.subplots() |
| 38 | +ax5.set_title('Horizontal Boxes') |
| 39 | +ax5.boxplot(data, vert=False, flierprops=red_square) |
| 40 | + |
| 41 | +fig6, ax6 = plt.subplots() |
| 42 | +ax6.set_title('Shorter Whisker Length') |
| 43 | +ax6.boxplot(data, flierprops=red_square, vert=False, whis=0.75) |
44 | 44 |
|
45 | 45 | # fake up some more data |
46 | | -spread= rand(50) * 100 |
47 | | -center = ones(25) * 40 |
48 | | -flier_high = rand(10) * 100 + 100 |
49 | | -flier_low = rand(10) * -100 |
50 | | -d2 = concatenate( (spread, center, flier_high, flier_low), 0 ) |
| 46 | +spread = np.random.rand(50) * 100 |
| 47 | +center = np.ones(25) * 40 |
| 48 | +flier_high = np.random.rand(10) * 100 + 100 |
| 49 | +flier_low = np.random.rand(10) * -100 |
| 50 | +d2 = np.concatenate((spread, center, flier_high, flier_low), 0) |
51 | 51 | data.shape = (-1, 1) |
52 | 52 | d2.shape = (-1, 1) |
53 | | -#data = concatenate( (data, d2), 1 ) |
| 53 | + |
54 | 54 | # Making a 2-D array only works if all the columns are the |
55 | 55 | # same length. If they are not, then use a list instead. |
56 | 56 | # This is actually more efficient because boxplot converts |
57 | 57 | # a 2-D array into a list of vectors internally anyway. |
58 | 58 | data = [data, d2, d2[::2,0]] |
59 | | -# multiple box plots on one figure |
60 | | -figure() |
61 | | -boxplot(data) |
62 | | -#savefig('box7') |
63 | | - |
64 | | -show() |
| 59 | +fig7, ax7 = plt.subplots() |
| 60 | +ax7.set_title('Multiple Samples with Different sizes') |
| 61 | +ax7.boxplot(data) |
65 | 62 |
|
| 63 | +plt.show() |
0 commit comments