Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 605fa97

Browse files
committed
Box plots with custom fill colors
1 parent f12d1e8 commit 605fa97

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

12 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Box plots with custom fill colors
2+
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
6+
# Random test data
7+
np.random.seed(123)
8+
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
9+
10+
fig, axes = plt.subplots(nrows=1,ncols=2, figsize=(12,5))
11+
12+
# rectangular box plot
13+
bplot1 = axes[0].boxplot(all_data,
14+
vert=True, # vertical box aligmnent
15+
patch_artist=True) # fill with color
16+
17+
# notch shape box plot
18+
bplot2 = axes[1].boxplot(all_data,
19+
notch=True, # notch shape
20+
vert=True, # vertical box aligmnent
21+
patch_artist=True) # fill with color
22+
23+
# fill with colors
24+
colors = ['pink', 'lightblue', 'lightgreen']
25+
for bplot in (bplot1, bplot2):
26+
for patch, color in zip(bplot['boxes'], colors):
27+
patch.set_facecolor(color)
28+
29+
# adding horizontal grid lines
30+
for ax in axes:
31+
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'],
39+
)
40+
41+
plt.show()
42+
43+

0 commit comments

Comments
 (0)