Skip to content

Commit 2665307

Browse files
committed
filled plots examples
1 parent dc208c0 commit 2665307

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

examples.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
def plot_error_bars_ex(ax):
5+
# example data
6+
x = np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0])
7+
y = np.exp(-x)
8+
xerr = 0.1
9+
yerr = 0.2
10+
11+
# lower & upper limits of the error
12+
lolims = np.array([0, 0, 1, 0, 1, 0, 0, 0, 1, 0], dtype=bool)
13+
uplims = np.array([0, 1, 0, 0, 0, 1, 0, 0, 0, 1], dtype=bool)
14+
ls = 'dotted'
15+
16+
# standard error bars
17+
ax.errorbar(x, y, xerr=xerr, yerr=yerr, linestyle=ls)
18+
19+
# including upper limits
20+
ax.errorbar(x, y + 0.5, xerr=xerr, yerr=yerr, uplims=uplims,
21+
linestyle=ls)
22+
23+
# including lower limits
24+
ax.errorbar(x, y + 1.0, xerr=xerr, yerr=yerr, lolims=lolims,
25+
linestyle=ls)
26+
27+
# including upper and lower limits
28+
ax.errorbar(x, y + 1.5, xerr=xerr, yerr=yerr,
29+
lolims=lolims, uplims=uplims,
30+
marker='o', markersize=8,
31+
linestyle=ls)
32+
33+
# Plot a series with lower and upper limits in both x & y
34+
# constant x-error with varying y-error
35+
xerr = 0.2
36+
yerr = np.full_like(x, 0.2)
37+
yerr[[3, 6]] = 0.3
38+
39+
# mock up some limits by modifying previous data
40+
xlolims = lolims
41+
xuplims = uplims
42+
lolims = np.zeros_like(x)
43+
uplims = np.zeros_like(x)
44+
lolims[[6]] = True # only limited at this index
45+
uplims[[3]] = True # only limited at this index
46+
47+
# do the plotting
48+
ax.errorbar(x, y + 2.1, xerr=xerr, yerr=yerr,
49+
xlolims=xlolims, xuplims=xuplims,
50+
uplims=uplims, lolims=lolims,
51+
marker='o', markersize=8,
52+
linestyle='none')
53+
54+
# tidy up the figure
55+
ax.set_xlim((0, 5.5))
56+
ax.set_title('Errorbar upper and lower limits')
57+
58+
59+
def plot_violin(ax):
60+
def adjacent_values(vals, q1, q3):
61+
upper_adjacent_value = q3 + (q3 - q1) * 1.5
62+
upper_adjacent_value = np.clip(upper_adjacent_value, q3, vals[-1])
63+
64+
lower_adjacent_value = q1 - (q3 - q1) * 1.5
65+
lower_adjacent_value = np.clip(lower_adjacent_value, vals[0], q1)
66+
return lower_adjacent_value, upper_adjacent_value
67+
68+
69+
def set_axis_style(ax, labels):
70+
ax.xaxis.set_tick_params(direction='out')
71+
ax.xaxis.set_ticks_position('bottom')
72+
ax.set_xticks(np.arange(1, len(labels) + 1))
73+
ax.set_xticklabels(labels)
74+
ax.set_xlim(0.25, len(labels) + 0.75)
75+
ax.set_xlabel('Sample name')
76+
77+
78+
# create test data
79+
np.random.seed(19680801)
80+
data = [sorted(np.random.normal(0, std, 100)) for std in range(1, 5)]
81+
82+
83+
84+
ax.set_title('Customized violin plot')
85+
parts = ax.violinplot(
86+
data, showmeans=False, showmedians=False,
87+
showextrema=False)
88+
89+
for pc in parts['bodies']:
90+
pc.set_facecolor('#D43F3A')
91+
pc.set_edgecolor('black')
92+
pc.set_alpha(1)
93+
94+
quartile1, medians, quartile3 = np.percentile(data, [25, 50, 75], axis=1)
95+
whiskers = np.array([
96+
adjacent_values(sorted_array, q1, q3)
97+
for sorted_array, q1, q3 in zip(data, quartile1, quartile3)])
98+
whiskers_min, whiskers_max = whiskers[:, 0], whiskers[:, 1]
99+
100+
inds = np.arange(1, len(medians) + 1)
101+
ax.scatter(inds, medians, marker='o', color='white', s=30, zorder=3)
102+
ax.vlines(inds, quartile1, quartile3, color='k', linestyle='-', lw=5)
103+
ax.vlines(inds, whiskers_min, whiskers_max, color='k', linestyle='-', lw=1)
104+
105+
# set style for the axes
106+
labels = ['A', 'B', 'C', 'D']
107+
set_axis_style(ax, labels)

test_show.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@
55

66
import matplotlib
77
matplotlib.use('module://pygame_matplotlib.backend_pygame')
8-
# matplotlib.use('Qt4Agg')
8+
#matplotlib.use('Qt4Agg')
99

1010
import matplotlib.pyplot as plt
1111
import matplotlib.figure as fg
1212

1313
import matplotlib.image as mpimg
1414

15-
fig, axes = plt.subplots(2,2,figsize=(16, 9))
15+
from examples import plot_error_bars_ex, plot_violin
16+
17+
fig, axes = plt.subplots(3,2,figsize=(16, 12))
1618
print(type(fig))
19+
1720
axes[0, 0].plot([1,2], [1,2], color='green')
1821
axes[0, 1].text(0.5, 0.5, '2', size=50)
1922
axes[1, 0].set_xlabel('swag')
23+
axes[1, 0].fill_between([0, 1, 2], [1, 2, 3], [3, 4, 5])
24+
axes[1, 0].scatter([0, 1, 2], [2, 3, 4], s=50)
2025
axes[1, 1].imshow(mpimg.imread('images' + os.sep + 'long_dog.jpg'))
26+
plot_error_bars_ex(axes[2, 1])
27+
plot_violin(axes[2, 0])
2128

2229

2330
plt.show()

0 commit comments

Comments
 (0)