|
1 | | -# Box plots with custom fill colors |
| 1 | +"""Box plots with custom fill colors. |
| 2 | +
|
| 3 | +This plot illustrates how to create two types of box plots |
| 4 | +(rectangular and notched), and how to fill them with custom |
| 5 | +colors by accessing the properties of the artists of the |
| 6 | +box plots. Additionally, the ``labels`` parameter is used to |
| 7 | +provide x-tick labels for each sample. |
| 8 | +""" |
2 | 9 |
|
3 | 10 | import matplotlib.pyplot as plt |
4 | 11 | import numpy as np |
5 | 12 |
|
6 | 13 | # Random test data |
7 | 14 | np.random.seed(123) |
8 | | -all_data = [np.random.normal(0, std, 100) for std in range(1, 4)] |
| 15 | +all_data = [np.random.normal(0, std, size=100) for std in range(1, 4)] |
| 16 | +labels = ['x1', 'x2', 'x3'] |
9 | 17 |
|
10 | 18 | fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 5)) |
11 | 19 |
|
12 | 20 | # rectangular box plot |
13 | 21 | bplot1 = axes[0].boxplot(all_data, |
14 | | - vert=True, # vertical box aligmnent |
15 | | - patch_artist=True) # fill with color |
| 22 | + vert=True, # vertical box alignment |
| 23 | + patch_artist=True, # fill with color |
| 24 | + labels=labels) # will be used to label x-ticks |
| 25 | +axes[0].set_title('Rectangular box plot') |
16 | 26 |
|
17 | 27 | # notch shape box plot |
18 | 28 | bplot2 = axes[1].boxplot(all_data, |
19 | 29 | notch=True, # notch shape |
20 | | - vert=True, # vertical box aligmnent |
21 | | - patch_artist=True) # fill with color |
| 30 | + vert=True, # vertical box alignment |
| 31 | + patch_artist=True, # fill with color |
| 32 | + labels=labels) # will be used to label x-ticks |
| 33 | +axes[1].set_title('Notched box plot') |
22 | 34 |
|
23 | 35 | # fill with colors |
24 | 36 | colors = ['pink', 'lightblue', 'lightgreen'] |
|
29 | 41 | # adding horizontal grid lines |
30 | 42 | for ax in axes: |
31 | 43 | ax.yaxis.grid(True) |
32 | | - ax.set_xticks([y+1 for y in range(len(all_data))], ) |
33 | | - ax.set_xlabel('xlabel') |
34 | | - ax.set_ylabel('ylabel') |
35 | | - |
36 | | -# add x-tick labels |
37 | | -plt.setp(axes, xticks=[y+1 for y in range(len(all_data))], |
38 | | - xticklabels=['x1', 'x2', 'x3', 'x4']) |
| 44 | + ax.set_xlabel('Three separate samples') |
| 45 | + ax.set_ylabel('Observed values') |
39 | 46 |
|
40 | 47 | plt.show() |
0 commit comments