|
| 1 | +""" |
| 2 | +Demo of errorbar function with different ways of specifying error bars. |
| 3 | +
|
| 4 | +Errors can be specified as a constant value (as shown in `errorbar_demo.py`), |
| 5 | +or as demonstrated in this example, they can be specified by an N x 1 or 2 x N, |
| 6 | +where N is the number of data points. |
| 7 | +
|
| 8 | +N x 1: |
| 9 | + Error varies for each point, but the error values are symmetric (i.e. the |
| 10 | + lower and upper values are equal). |
| 11 | +
|
| 12 | +2 x N: |
| 13 | + Error varies for each point, and the lower and upper limits (in that order) |
| 14 | + are different (asymmetric case) |
| 15 | +
|
| 16 | +In addition, this example demonstrates how to use log scale with errorbar. |
| 17 | +""" |
| 18 | +import numpy as np |
| 19 | +import matplotlib.pyplot as plt |
| 20 | + |
| 21 | +# example data |
| 22 | +x = np.arange(0.1, 4, 0.5) |
| 23 | +y = np.exp(-x) |
| 24 | +# example error bar values that vary with x-position |
| 25 | +error = 0.1 + 0.2 * x |
| 26 | +# error bar values w/ different -/+ errors |
| 27 | +lower_error = 0.4 * error |
| 28 | +upper_error = error |
| 29 | +asymmetric_error = [lower_error, upper_error] |
| 30 | + |
| 31 | +fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True) |
| 32 | +ax0.errorbar(x, y, yerr=error, fmt='-o') |
| 33 | +ax0.set_title('variable, symmetric error') |
| 34 | + |
| 35 | +ax1.errorbar(x, y, xerr=asymmetric_error, fmt='o') |
| 36 | +ax1.set_title('variable, asymmetric error') |
| 37 | +ax1.set_yscale('log') |
| 38 | +plt.show() |
| 39 | + |
0 commit comments