|
1 | 1 | #!/usr/bin/env python |
2 | | -from pylab import * |
| 2 | +import numpy as np |
| 3 | +import matplotlib.pyplot as plt |
3 | 4 |
|
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) |
10 | 8 |
|
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 |
13 | 12 |
|
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") |
16 | 17 |
|
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') |
19 | 23 |
|
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) |
22 | 26 |
|
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') |
25 | 30 |
|
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') |
28 | 34 |
|
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 |
31 | 40 |
|
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') |
34 | 44 |
|
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') |
40 | 46 |
|
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() |
45 | 48 |
|
46 | | -show() |
|
0 commit comments