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

Skip to content

Commit 6ca4756

Browse files
committed
errorbar_demo: fewer figures, better style
svn path=/trunk/matplotlib/; revision=8368
1 parent 6dbedd7 commit 6ca4756

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

examples/pylab_examples/errorbar_demo.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
#!/usr/bin/env python
2-
from pylab import *
2+
import numpy as np
3+
import matplotlib.pyplot as plt
34

4-
t = arange(0.1, 4, 0.1)
5-
s = exp(-t)
6-
e = 0.1*abs(randn(len(s)))
7-
f = 0.1*abs(randn(len(s)))
8-
g = 2*e
9-
h = 2*f
5+
# example data
6+
x = np.arange(0.1, 4, 0.5)
7+
y = np.exp(-x)
108

11-
figure()
12-
errorbar(t, s, e, fmt='o') # vertical symmetric
9+
# example variable error bar values
10+
yerr = 0.1 + 0.2*np.sqrt(x)
11+
xerr = 0.1 + yerr
1312

14-
figure()
15-
errorbar(t, s, None, f, fmt='o') # horizontal symmetric
13+
# First illustrate basic pyplot interface, using defaults where possible.
14+
plt.figure()
15+
plt.errorbar(x, y, xerr=0.2, yerr=0.4)
16+
plt.title("Simplest errorbars, 0.2 in x, 0.4 in y")
1617

17-
figure()
18-
errorbar(t, s, e, f, fmt='o') # both symmetric
18+
# Now switch to a more OO interface to exercise more features.
19+
fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True)
20+
ax = axs[0,0]
21+
ax.errorbar(x, y, yerr=yerr, fmt='o')
22+
ax.set_title('Vert. symmetric')
1923

20-
figure()
21-
errorbar(t, s, [e,g], [f,h], fmt='--o') # both asymmetric
24+
# With 4 subplots, reduce the number of axis ticks to avoid crowding.
25+
ax.locator_params(nbins=4)
2226

23-
figure()
24-
errorbar(t, s, [e,g], f, fmt='o', ecolor='g') # both mixed
27+
ax = axs[0,1]
28+
ax.errorbar(x, y, xerr=xerr, fmt='o')
29+
ax.set_title('Hor. symmetric')
2530

26-
figure()
27-
errorbar(t, s, e, [f,h], fmt='o') # both mixed
31+
ax = axs[1,0]
32+
ax.errorbar(x, y, yerr=[yerr, 2*yerr], xerr=[xerr, 2*xerr], fmt='--o')
33+
ax.set_title('H, V asymmetric')
2834

29-
figure()
30-
errorbar(t, s, [e,g], fmt='o') # vertical asymmetric
35+
ax = axs[1,1]
36+
ax.set_yscale('log')
37+
# Here we have to be careful to keep all y values positive:
38+
ylower = np.maximum(1e-2, y - yerr)
39+
yerr_lower = y - ylower
3140

32-
figure()
33-
errorbar(t, s, yerr=e, fmt='o') # named
41+
ax.errorbar(x, y, yerr=[yerr_lower, 2*yerr], xerr=xerr,
42+
fmt='o', ecolor='g')
43+
ax.set_title('Mixed sym., log y')
3444

35-
figure()
36-
errorbar(t, s, xerr=f, fmt='o') # named
37-
xlabel('Distance (m)')
38-
ylabel('Height (m)')
39-
title('Mean and standard error as a function of distance')
45+
fig.suptitle('Variable errorbars')
4046

41-
figure()
42-
ax = subplot(111)
43-
ax.set_yscale('log')
44-
errorbar(t, s+2, e, f, fmt='o') # both symmetric
47+
plt.show()
4548

46-
show()

0 commit comments

Comments
 (0)