Matplotlib Errorbar For Lines and Graphs

In this article, we learn about the Matplotlib errorbar in Python. The Pyplot module of the Matplotlib library provides MATLAB-like interface. And the matplotlib.pyplot.errorbar() function plots y versus x as lines and/or markers with attached errorbars. Moreover, error bars help indicate estimated error or uncertainty to give a general sense of how precise a measurement is; this is done through the use of markers drawn over the original graph and its data points.

Before we look into examples of Matplotlib errorbars and errorbar graphs, let me brief you on the syntax and return the same.

Syantax of matplotlib errorbar()

matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt=”, ecolor=None, elinewidth=None, capsize=None, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, errorevery=1, capthick=None, *, data=None, **kwargs)

Parameters of Matplotlib Errorbar:

  • x, y: The horizontal and vertical coordinates of the data points.
  • fmt: Contains the string value (Optional)
  • xerr, yerr: An array containing errors, and the errors must be positive values.
  • ecolor: (default: NONE) In simple words, it’s the color of the errorbar lines. (Optional)
  • elinewidth: Linewidth of the errorbar lines with default value NONE. (Optional)
  • capsize: Length of the error bar caps in points with default value NONE. (Optional)
  • barsabove: (default: False)Contains boolean values for plotting error bars above the plot symbols. (Optional)
  • lolims, uplims, xlolims, xuplims: Contains boolean values, which indicate that a value gives only upper/lower limits. (Optional)
  • errorevery: Contain integer values that help draws error bars on a subset of the data. (Optional)

Return type of Matplotlib Errorbar:

The Matplotlib errorbar() function returns a container having:

  • plotline: Line2D instance of x, y plot markers and/or line.
  • caplines: Tuple of Line2D instances of the error bar caps.
  • barlinecols: Tuple of LineCollection with the horizontal and vertical error ranges.

Example of Matplotlib Errorbar in Python

Errorbar helps you to understand the nature of graphs and their convergence. The errorbar can give an idea about the graph’s variance with its mean. Correspondingly, you can identify and take care of the outliers from your graph. Moreover, it represents uncertainty in your graph.

import numpy as np 
import matplotlib.pyplot as plt 
  
xval = np.arange(0.1, 4, 0.5) 
yval = np.exp(-xval) 
  
plt.errorbar(xval, yval, xerr = 0.4, yerr = 0.5) 
  
plt.title('matplotlib.pyplot.errorbar() function Example') 
plt.show() 

OUTPUT:

Example of Matplotlib Errorbar in Python

Explanation:

Firstly, the above example is a fundamental implementation of the Matplotlib errorbar() function. The ‘xval’ and ‘yval’ contains the horizontal and vertical coordinates of the data points required for errorbar plotting. In addition, the ‘xerr’ and ‘yerr’ are arguments to the errorbar function. The values assigned to them are 0.4 and 0.5, respectively, the positive error values. Finally, these four parameters passed to the errorbar() function are mandatory arguments required to plot any Python errorbars.

Matplotlib Errorbar in Python Multiple lines

It’s very important to be able to plot multiple lines in the same graphs. In the following example, we’ll plot multiple errorbars in the same graph.

import numpy as np 
import matplotlib.pyplot as plt 
   
fig = plt.figure() 
x = np.arange(10) 
y = 3 * np.sin(x / 20 * np.pi) 
yerr = np.linspace(0.05, 0.2, 10) 
   
plt.errorbar(x, y + 7, yerr = yerr, 
             label ='Line1') 
plt.errorbar(x, y + 5, yerr = yerr, 
             uplims = True,  
             label ='Line2') 
plt.errorbar(x, y + 3, yerr = yerr,  
             uplims = True,  
             lolims = True, 
             label ='Line3') 
  
upperlimits = [True, False] * 5
lowerlimits = [False, True] * 5
plt.errorbar(x, y, yerr = yerr, 
             uplims = upperlimits,  
             lolims = lowerlimits, 
             label ='Line4') 
   
plt.legend(loc ='upper left') 
  
plt.title('matplotlib.pyplot.errorbar()\ 
function Example') 
plt.show() 

OUTPUT:

Errorbar in Python Multiple lines

Explanation:

Firstly, in the above example, the matplotlib errorbar() function creates multiple errorbar lines. The x and y defined are the coordinates of the data points. Along the y-axis, graph plot error. Hence only ‘yerr’ is specified for each line. Additionally, the upper and lower limits indicate the values for only upper and lower limits. Also, the label is an argument indicating the lines as Line1, Line2, etc.

Errorbar graph with error in x

import matplotlib.pyplot as plt  
  
# making a simple plot 
x =[1, 2, 3, 4, 5, 6, 7] 
y =[1, 2, 1, 2, 1, 2, 1] 
  
# creating error 
x_error = 0.5
  
# ploting graph 
plt.plot(x, y) 
plt.errorbar(x, y, 
             xerr = x_error, 
             fmt ='o') 

OUTPUT:

Errorbar graph with error in x

Explanation:

Firstly, in the above example, x and y are arrays defined containing x and y coordinates to plot the graph. Additionally, the ‘x_error’ is 0.5, which introduces errors only along the x-axis. The x, y, and xerr are the mandatory parameters passed to the Matplotlib errorbar in Python. The optional parameter ‘fmt’ containing string value is also passed.

Errorbar graph with error in y

import matplotlib.pyplot as plt  
  
  
# making a simple plot 
x =[1, 2, 3, 4, 5, 6, 7] 
y =[1, 2, 1, 2, 1, 2, 1] 
  
# creating error 
y_error = 0.2
  
# ploting graph 
plt.plot(x, y) 
  
plt.errorbar(x, y, 
             yerr = y_error, 
             fmt ='o') 

OUTPUT:

Errorbar graph with error in y

Explanation:

Firstly, in the above example, x and y are arrays defined containing x and y coordinates to plot the graph. The ‘y_error’ is 0.2, which introduces errors only along the y-axis. The x, y, and yerr are the mandatory parameters passed to the errorbar function. The optional parameter ‘fmt’ containing string value is also passed.

Errorbar graph with Variable error in x and y

import matplotlib.pyplot as plt 
  
x =[1, 2, 3, 4, 5] 
y =[1, 2, 1, 2, 1] 
  
y_errormin =[0.1, 0.5, 0.9, 
             0.1, 0.9] 
y_errormax =[0.2, 0.4, 0.6,  
             0.4, 0.2] 
  
x_error = 0.5
y_error =[y_errormin, y_errormax] 
  
# ploting graph 
# plt.plot(x, y) 
plt.errorbar(x, y, 
             yerr = y_error, 
             xerr = x_error,  
             fmt ='o') 

OUTPUT:

Errorbar graph with Variable error in x and y

Explanation:

The above example depicts an error bar graph plot for variable errors. Likewise, the x and y are two arrays defined containing the coordinate points. The error along the x-axis is 0.5. Simultaneously, the error along the y-axis between two arrays is y_errormin and y_errormax. These parameters are arguments to the Matplotlib errorbar function to get the desired output.

Conclusion

This article clearly presents to you different ways of using the Matplotlib errorbar in Python. Examples of both errorbar lines and graphs are provided with a detailed explanation. Errorbars provide an additional layer of detail on the presented data. Refer to this article for any queries related to the Matplotlib errorbar() function.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments