When plotting experimental data it is customary to include error bars that indicate graphically the degree of uncertainty that exists in the measurement of each data point. The matplotlib function errorbar plots data with error bars attached. It can be used in a way that either replaces or augments the plot function. Both vertical and horizontal error bars can be displayed.
When error bars are desired, you typically replace the plot function with the errorbar function. The first two arguments of the errorbar function are the x and y arrays to be plotted, just as for the plot function. The keyword fmt must be used to specify the format of the points to be plotted; the format specifiers are the same as for plot.
The keywords xerr and yerr are used to specify the x and y error bars. Setting one or both of them to a constant specifies one size for all the error bars. Alternatively, setting one or both of them equal to an array that has the same length as the x and y arrays allows you to give each data point an error bar with a different value. If you only want y error bars, then you should only specify the yerr keyword and omit the xerr keyword. The color of the error bars is set with the keyword ecolor.
The code below illustrates how to make error bars:
import numpy as np
import matplotlib.pyplot as plt
# read data from file
xdata, ydata, yerror = np.loadtxt('expDecayData.txt',unpack=True)
# create theoretical fitting curve
x = np.linspace(0, 45, 128)
y = 1.1 + 3.0*x*np.exp(-(x/10.0)**2)
# create plot
plt.figure(1, figsize=(6, 4))
plt.plot(x, y, '-C0', label="theory")
plt.errorbar(xdata, ydata, fmt='oC1', label="data",
xerr=0.75, yerr=yerror, ecolor='black')
plt.xlabel('x')
plt.ylabel('transverse displacement')
plt.legend(loc='upper right')
# save plot to file
plt.savefig('figures/ExpDecay.pdf')
# display plot on screen
plt.show()
Lines 15 and 16 contain the call to the errorbar function. The x error bars are all set to a constant value of 0.75, meaning that the error bars extend 0.75 to the left and 0.75 to the right of each data point. The y error bars are set equal to an array, which was read in from the data file containing the data to be plotted, so each data point has a different y error bar. By the way, leaving out the xerr keyword argument in the errorbar function call below would mean that only the y error bars would be plotted. The resulting plot is shown below:
When error bars are desired, you typically replace the plot function with the errorbar function. The first two arguments of the errorbar function are the x and y arrays to be plotted, just as for the plot function. The keyword fmt must be used to specify the format of the points to be plotted; the format specifiers are the same as for plot.
The keywords xerr and yerr are used to specify the x and y error bars. Setting one or both of them to a constant specifies one size for all the error bars. Alternatively, setting one or both of them equal to an array that has the same length as the x and y arrays allows you to give each data point an error bar with a different value. If you only want y error bars, then you should only specify the yerr keyword and omit the xerr keyword. The color of the error bars is set with the keyword ecolor.
The code below illustrates how to make error bars:
import numpy as np
import matplotlib.pyplot as plt
# read data from file
xdata, ydata, yerror = np.loadtxt('expDecayData.txt',unpack=True)
# create theoretical fitting curve
x = np.linspace(0, 45, 128)
y = 1.1 + 3.0*x*np.exp(-(x/10.0)**2)
# create plot
plt.figure(1, figsize=(6, 4))
plt.plot(x, y, '-C0', label="theory")
plt.errorbar(xdata, ydata, fmt='oC1', label="data",
xerr=0.75, yerr=yerror, ecolor='black')
plt.xlabel('x')
plt.ylabel('transverse displacement')
plt.legend(loc='upper right')
# save plot to file
plt.savefig('figures/ExpDecay.pdf')
# display plot on screen
plt.show()
Lines 15 and 16 contain the call to the errorbar function. The x error bars are all set to a constant value of 0.75, meaning that the error bars extend 0.75 to the left and 0.75 to the right of each data point. The y error bars are set equal to an array, which was read in from the data file containing the data to be plotted, so each data point has a different y error bar. By the way, leaving out the xerr keyword argument in the errorbar function call below would mean that only the y error bars would be plotted. The resulting plot is shown below:
0 comments:
Post a Comment