|
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 boxplots |
| 4 | +(rectangular and notched), and how to fill them with custom |
| 5 | +colors by accessing the properties of the artists of the |
| 6 | +boxplots. 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, 4)) 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 aligmnent |
| 23 | + patch_artist=True, # fill with color |
| 24 | + labels=labels) # will be used to label x-ticks |
16 | 25 |
|
17 | 26 | # notch shape box plot
|
18 | 27 | bplot2 = axes[1].boxplot(all_data,
|
19 | 28 | notch=True, # notch shape
|
20 |
| - vert=True, # vertical box aligmnent |
21 |
| - patch_artist=True) # fill with color |
| 29 | + vert=True, # vertical box aligmnent |
| 30 | + patch_artist=True, # fill with color |
| 31 | + labels=labels) # will be used to label x-ticks |
22 | 32 |
|
23 | 33 | # fill with colors
|
24 | 34 | colors = ['pink', 'lightblue', 'lightgreen']
|
|
29 | 39 | # adding horizontal grid lines
|
30 | 40 | for ax in axes:
|
31 | 41 | 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']) |
| 42 | + ax.set_xlabel('Three Separate Samples') |
| 43 | + ax.set_ylabel('Observed values') |
39 | 44 |
|
40 | 45 | plt.show()
|
0 commit comments