Mean-and-error plots

For experimental sciences, a data point is often averaged from several repeats of experiments, necessitating the need to show the error range to illustrate the precision level. In this case, mean-and-error plots may be more suitable than bar charts. In Matplotlib, mean-and-error plots are generated by the plt.errorbar()API.

When the positive errors and negative errors are the same, we can input 1D arrays to error values to draw symmetric error bars. Otherwise, we input 2D arrays of [positive errors, negative errors] for asymmetric error bars. While it is more common to have plots with y errors only, error values for both x and y axes are supported.

By default, Matplotlib draws a line linking each error bar, with format fmt set to '.-'. For discrete datasets, we can add the keyword argument fmt='.' to remove the line. Let's go through a simple example:

import matplotlib.pyplot as plt
import numpy as np

# Prepare data for a sine curve
x = np.arange(0, 5, 0.3)
y = np.sin(-x)

# Prepare random error to plot the error bar
np.random.seed(100)
e1 = 0.1 * np.abs(np.random.randn(len(y)))

# Plotting the error bar
plt.errorbar(x, y, yerr=e1, fmt='.-')
plt.show()

We now get a sine curve with error bars, as follows. Try to substitute it with some real testing data you get:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.127.161