Matplotlib plot error bars

In this Python tutorial, we will discuss Matplotlib plot error bars in python. Here we will cover different examples related to error bars using matplotlib. And we will also cover the following topics:

  • Matplotlib plot error bars
  • Matplotlib plot error bars example
  • Matplotlib interactive plot error bars
  • Matplotlib chart error bars
  • Matplotlib scatter plot error bars
  • Matplotlib plot_date error bars
  • Matplotlib plot only error bars
  • Matplotlib plot error bars symmetric
  • Matplotlib plot error bars asymmetric
  • Matplotlib polar plot error bars

Matplotlib plot error bars

In this section, we are going to learn about the error bar. Before starting error bars firstly, we understand what does error means.

Error is a mistake or we can say that difference between the calculated value and actual value.

When we graphical represent the data, some of the data have irregularity. To indicate these irregularities or uncertainties we use Error Bars.

Basically, error bars are used to represent errors in the graphical plot.

The following steps are used to plot error bars in matplotlib which is outlined below:

  • Defining Libraries: Import the libraries which are required to plot error bars (For data creation and manipulation: Numpy, For data visualization: pyplot from matplotlib).
  • Define X and Y: Define the data values used for plotting. Data values of x-axis and y-axis.
  • Plot error bars: By using the errorbar() method we can plot the error bars.
  • Display: Finally we have to use the show() method to display the plot.

The syntax to plot error bars is as below:

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

The above-used parameters are outlined as below:

  • x: specifies horizontal coordinates of the data points.
  • y: specifies vertical coordinates of the data points.
  • xerr: Define the horizontal error bar sizes. Must have a float or array-like shape.
  • yerr: Define the vertical error bar sizes. Must have a float or array-like shape.
  • fmt: Contains string value. By default, this plot error bars with markers. Use ‘none’ to plot error bars without markers.
  • ecolor: specifies the color of the error bars.
  • elinewidth: specifies linewidth of the error bars.
  • capsize: specifies the length of error bars in points or float.
  • capthick: specifies the thickness of error bars cap in float or points.
  • barsabove: It contains bool value. By default value is False, if the value is True error bars are plotted above the plot symbol.
  • lolims,uplims,xlolims,xuplims: specifies that value gives only upper and lower limits. It contains bool value.
  • errorevery: It contains integer values and is used to draw error bars on the subset of the data.

First, learn “How to install matplotlib python“.

Matplotlib plot error bars example

In the above sections, we discussed what does error and error bars mean. And we also discussed what are the various steps used to plot error bars.

Let’s understand the concept with the help of an example as below:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data

x= [1, 2, 3, 5]
y= [9, 15, 20, 25]

# Plot error bar

plt.errorbar(x, y, xerr = 0.9)

# Display graph

plt.show()
  • In the above, example we import the matplotlib.pyplot library.
  • Then we define the x-axis and y-axis data points.
  • plt.errorbar() method is used to plot error bars and we pass the argument x, y, and xerr and set the value of xerr = 0.9.
  • Then we use plt.show() method to display the error bar plotted graph.
Matplotlib plot error bars example
plt.errorbar()

Read: Matplotlib plot a line

Matplotlib plot interactive error bars

Here we format the error bars or we can say that customizing the error bar according to our choice to become our error bar more interactive.

Let’s change the following things in the error bars to become it more interactive:

  • fmt: change style of marker. Set it to circle.
  • color: change the color of the marker. Set it to orange.
  • ecolor: change the color of the error bar. Set it to lightgreen.
  • elinewidth: change the line width of the error bar. Set it to 5.
  • capsize: change the capsize of the error bar. Set it to 10.

Understand the concept with the help of an example:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data

x= [1, 2, 3, 5]
y= [9, 15, 20, 25]

# Plot error bar

plt.errorbar(x, y, xerr = 0.9, fmt = 'o',color = 'orange', 
            ecolor = 'lightgreen', elinewidth = 5, capsize=10)

# Display graph

plt.show()
  • In the above example, we plot the error bars and format them according to above mention list.
  • We use plt.errorbar() method to plot error bars and become it more interactive.
Matplotlib plot error bar formatting
plt.errorbar()

Read: Python plot multiple lines using Matplotlib

READ:  PyTorch nn linear + Examples

Matplotlib chart error bars

In this section, we will create a chart plot with error bars using Matplotlib. We use plt.errorbar() method to plot error bars in bar charts.

The following are the cases in the bar chart in which we draw error bars:

  • Error in x values
  • Error in y values
  • Error in both x and y values

Matplotlib chart error bars in x values

By using the plt.errorbar() method we plot the error bars and pass the argument xerr to plot error on the x values.

The syntax for this is:

matplotlib.pyplot.errorbar(x, y, xerr=None)

Let’s understand this concept with the help of an example:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data

x= [6, 15, 2.3, 9]
y= [9, 15, 20, 25]

# Define Error

x_error = [2.3, 5.1, 1, 3.1]

# Plot Bar chart

plt.bar(x,y)

# Plot error bar

plt.errorbar(x, y, xerr = x_error,fmt='o',ecolor = 'red',color='yellow')

# Display graph

plt.show()
  • In the above example, we import matplotlib.pyplot library and define the data point on the x-axis and y-axis.
  • Then we define the error value and use the plt.bar() method to plot a bar chart.
  • plt.errorbar() method is used to plot error bars and we pass xerr as an argument and set its value to be x_error value which we define.
Matplotlib chart error bar
plt.errorbar(xerr=None)

Matplotlib chart error bars in y values

By using the plt.errorbar() method we plot the error bars and pass the argument yerr to plot error on the y values.

The syntax to plot error bars on y values is as given below:

matplotlib.pyplot.errorbar(x, y, yerr=None)

Let’s take an example for better understanding:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data

x= [6, 15, 2.3, 9]
y= [9, 15, 20, 25]

# Define Error

y_error = [2.3, 5.1, 1, 3.1]

# Plot Bar chart

plt.bar(x,y)

# Plot error bar

plt.errorbar(x, y, yerr = y_error,fmt='o',ecolor = 'red',color='yellow')

# Display graph

plt.show()
  • In the above example, we import matplotlib.pyplot library
  • After this defines the data point on the x-axis and y-axis.
  • Then we define the error value and use the plt.bar() method to plot a bar chart and use plt.errorbar() method is used to plot error bars.
  • Pass yerr as an argument and set its value to equal y_error value which we define.
Matplotlib chart error bars
plt.errorbar(yerr=None)

Matplotlib chart error bars in x and y values

By using the plt.errorbar() method we plot the error bars and pass the argument xeer and yerr to plot error on both x and y values respectively.

The syntax to plot error bars on both the values is as given below:

matplotlib.pyplot.errorbar(x, y, xerr=None, yerr=None)

Let’s take an example to know how to plot errorbars on both values:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data

x= [6, 15, 2.3, 9]
y= [9, 15, 20, 25]

# Define Error

x_error = [4.2, 6, 10, 8.6]
y_error = [2.3, 5.1, 1, 3.1]

# Plot Bar chart

plt.bar(x,y)

# Plot error bar

plt.errorbar(x, y, xerr = x_error, yerr = y_error,
             fmt='o', ecolor = 'red',color='yellow')

# Display graph


plt.show()
  • In the above example, we import matplotlib.pyplot library
  • After this defines the data point on the x-axis and y-axis.
  • Then we define the error value and use the plt.bar() method to plot a bar chart and use plt.errorbar() method is used to plot error bars.
  • Pass xeer, yerr as an argument and set its value to equal x_error, y_error value respectively which we define.
Matplotlib error bar chart
plt.errorbar(xerr=None,yerr=None)

Read: What is matplotlib inline

Matplotlib scatter plot error bars

In this section, we will create a scatter plot with error bars using Matplotlib. We use plt.errorbar() method to plot error bars in scatter plot.

The following are the cases in the scatter plot in which we draw error bars:

  • Error in x values
  • Error in y values
  • Error in both x and y values

Matplotlib scatter plot error bars in x values

By using the plt.errorbar() method we plot the error bars and pass the argument xerr to plot error on the x values in the scatter plot.

The syntax for this is:

matplotlib.pyplot.errorbar(x, y, xerr=None)

Let’s understand this concept with the help of an example:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data

x= [10, 20, 30, 40]
y= [4, 8, 12, 16]

# Define Error

x_error = [2, 4, 6, 8]

# Plot scatter plot

plt.scatter(x,y)

# Plot error bar

plt.errorbar(x, y, xerr = x_error,fmt='o',ecolor = 'cyan',color='black')

# Display graph

plt.show()
  • In the above example, we import matplotlib.pyplot library and define the data point on the x-axis and y-axis.
  • Then we define the error value and use the plt.scatter() method to draw a scatter plot.
  • plt.errorbar() method is used to plot error bars and we pass xerr as an argument and set its value to be x_error value which we define.
Matplotlib scatter plot error bar
plt.errorbar(xerr=None)

Matplotlib scatter plot error bars in y values

By using the plt.errorbar() method we plot the error bars and pass the argument yerr to plot error on the y values in the scatter plot.

READ:  Python Turtle 3d Shapes

The syntax to plot error bars on y values is as given below:

matplotlib.pyplot.errorbar(x, y, yerr=None)

Let’s take an example for better understanding:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data

x= [10, 20, 30, 40]
y= [4, 8, 12, 16]

# Define Error

y_error = [2, 4, 6, 8]

# Plot Scatter Plot

plt.scatter(x,y)

# Plot error bar

plt.errorbar(x, y, yerr = y_error,fmt='o',ecolor = 'cyan',color='black')

# Display graph

plt.show()
  • In the above example, we import matplotlib.pyplot library
  • After this defines the data point on the x-axis and y-axis.
  • Then we define the error value and use the plt.scatter() method to plot a scatter plot and use plt.errorbar() method is used to plot error bars.
  • Pass yerr as an argument and set its value to equal y_error value which we define.
Matplotlib scatter plot having error bar
plt.errorbar(yerr=None)

Matplotlib scatter plot error bars in x and y values

By using the plt.errorbar() method we plot the error bars and pass the argument xeer and yerr to plot error on both x and y values respectively.

The syntax to plot error bars on both the values is as given below:

matplotlib.pyplot.errorbar(x, y, xerr=None, yerr=None)

Let’s take an example to know how to plot errorbars on both values:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data


x= [10, 20, 30, 40]
y= [4, 8, 12, 16]

# Define Error

x_error= [3, 6, 9, 12]
y_error = [2, 4, 6, 8]

# Plot Scatter Plot

plt.scatter(x,y)

#Plot error bar

plt.errorbar(x, y, xerr= x_error, yerr = y_error,fmt='o',ecolor = 'cyan',color='black')

# Display graph

plt.show()
  • In the above example, we import matplotlib.pyplot library
  • After this defines the data point on the x-axis and y-axis.
  • Then we define the error value and use the plt.scatter() method to plot a scatter plot and use plt.errorbar() method is used to plot error bars.
  • Pass xeer, yerr as an argument and set its value to equal x_error, y_error value which we define.
Matplotlib scatter error bars
plt.errorbar(xerr=None, yerr=None)

Read: Matplotlib plot bar chart

Matplotlib plot_date error bars

In this section, we will create a dates plot with error bars using Matplotlib. We use plt.errorbar() method to plot error bars.

The following are the cases in the dates plot in which we draw error bars:

  • Error in x values
  • Error in y values
  • Error in both x and y values

Matplotlib plot_date error bars in x values

By using the plt.errorbar() method we plot the error bars and pass the argument xerr to plot error on the x values in the date plot.

The syntax for this is:

matplotlib.pyplot.errorbar(x, y, xerr=None)

Let’s understand this concept with the help of an example:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data

x= [1, 2, 3]
y= [1.5, 2, 2.5]

# Define Error

x_error= 1.5

# Plot Date

plt.plot_date(x,y)

#Plot error bar

plt.errorbar(x, y, xerr= x_error, fmt='o',
             ecolor = 'lightblue',color='m')

# Display graph


plt.tight_layout()

plt.show()
  • In the above example, we import matplotlib.pyplot library and define the data point on the x-axis and y-axis.
  • Then we define the error value and use the plt.plot_date() method to draw a plot consisting dates.
  • plt.errorbar() method is used to plot error bars and we pass xerr as an argument and set its value to be x_error value which we define.
Matplotlib plot_date error bar
plt.errorbar(xerr=None)

Read: Matplotlib subplot tutorial

Matplotlib plot_date error bars in y values

By using the plt.errorbar() method, we plot the error bars and pass the argument yerr to plot the error on the y values in the date plot.

The syntax to plot error bars on y values is as given below:

matplotlib.pyplot.errorbar(x, y, yerr=None)

Let’s take an example for better understanding:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data


x= [1, 2, 3]
y= [0.5, 0.8, 1]

# Define Error


y_error= 0.1

# Plot Date

plt.plot_date(x,y)

# Plot error bar

plt.errorbar(x, y, yerr= y_error, fmt='o',
             ecolor = 'lightblue',color='m')

# Display graph

plt.tick_params(axis='x', which='major', labelsize=8)

plt.tight_layout()

plt.show()
  • In the above example, we import matplotlib.pyplot library
  • After this defines the data point on the x-axis and y-axis.
  • Then we define the error value and use the plt.plot_date() method to plot a graph consisting of dates and use plt.errorbar() method is used to plot error bars.
  • Pass yerr as an argument and set its value to equal y_error value which we define.
Matplotlib plot_date having error bars
plt.errorbar(yerr=None)

Matplotlib plot_date error bars in x and y values

By using the plt.errorbar() method we plot the error bars and pass the argument xeer and yerr to plot error on both x and y values respectively in the date plot.

The syntax to plot error bars on both the values is as given below:

matplotlib.pyplot.errorbar(x, y, xerr=None, yerr=None)

Let’s take an example to know how to plot errorbars on both valuesin date plot:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data

x= [1,2,3]
y= [1.5, 2, 2.5]

# Define Error


x_error= 1.5
y_error = 2.3

# Plot Date

plt.plot_date(x,y)

# Plot error bar


plt.errorbar(x, y, xerr= x_error, yerr = y_error,fmt='o',ecolor = 'lightblue',color='m')

# Display graph


plt.tight_layout()

plt.show()
  • In the above example, we import matplotlib.pyplot library
  • After this defines the data point on the x-axis and y-axis.
  • Then we define the error value and use the plt.plot_date() method to plot a dates and use plt.errorbar() method is used to plot error bars.
  • Pass xeer, yerr as an argument and set its value to equal x_error, y_error value which we define.
Matplotlib plot_date consist of error bars
plt.errorbar(xerr=None,yerr=None)

Read: Matplotlib best fit line

READ:  How to Add Column from Another Dataframe in Pandas Python [6 Methods]

Matplotlib plot only error bars

Here we learn about how we can plot error bars only.

The syntax to plot error bars is:

matplotlib.pyplot.errorbar(x,y,xerr=None,yerr=None)

The parameters used above are:

  • x: horizontal coordinates of the data points.
  • y: vertical coordinates of the data points.
  • xerr: Define the horizontal error bar sizes. Must have a float or array-like shape.
  • yerr: Define the vertical error bar sizes. Must have a float or array-like shape.

Let’s take an example to create only error bars:

# Import Library

import matplotlib.pyplot as plt
  
# Define Data

x= [1, 2, 3]
y= [1.5, 2, 2.5]

# Define Error

x_error = 0.2
y_error = 0.5

#Plot error bar

plt.errorbar(x, y, xerr= x_error, yerr = y_error,fmt='o',
             ecolor = 'pink',color='blue')

# Display graph

plt.show()

In the above example, we define the data and error values, and by using the errorbar() method we plot the only error bar graph.

Matplotlib plot only error bars
plt.errorbar()

Read: Matplotlib subplots_adjust

Matplotlib plot error bars symmetric

Here we learn what does symmetric error bars are. Errors are constant values and we represent the error values in the array form.

Sometimes, we have a case in which an error differed for each point, but the lower and upper values of error are equal. That type of case is known as the symmetric error bar.

In the symmetric case, the shape of an array is in the form: (N,)

Let’s see the example of symmetric error bars:

# Import library

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x = np.arange(2, 16, 1.5)
y = np.exp(x*2)

# Define Data

error = 0.1 * x

# Plot error

plt.errorbar(x, y, yerr=error, fmt='-o')

# Display Graph

plt.show()
  • In the above example, we define data using NumPy arrange() method and exp() method. After that, we define error value in the shape of (N,).
  • By using plt.errorbar() method, we plot the symmetric error bars.
Matplotlib plot erro bars symmetric
Shape(N,) Symmetric in Nature

Read: Matplotlib log log plot

Matplotlib plot error bars asymmetric

Here we learn what does asymmetric error bars are. Errors are constant values and they are represented in the array form.

Sometimes, we have a case in which an error differed for each point, but the lower and upper limits of error are unequal or different in nature. That type of case is known as the asymmetric error bar.

In the asymmetric case, the shape of an array is in the form: (2, N).

Let’s see the example of asymmetric error bars:

# Import Libraries

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x = np.arange(2, 16, 1.5)
y = np.exp(x*2)

# Define errors

error = 0.1 * x

# Lower limit of error

lower_error = 0.6 * error

# Upper limit of error

upper_error = error

# plot error bars

asymmetric_error = [lower_error, upper_error]
plt.errorbar(x, y, xerr=asymmetric_error, fmt='o')

# Display Graph

plt.show()

In the above example, we define different values of error of lower and upper limits. By using the plt.errorbar() method we plot error bars in asymmetric nature.

Matplotlib plot error bars asymmetric
Shape(2,N) Asymmetric in Nature

Read: Matplotlib plot_date

Matplotlib polar plot error bars

A Cartesian coordinate system is plotted using the polar coordinates. plt.polar() method is used to plot the polar coordinates.

The syntax to plot polar plot is as given below:

matplotlib.pyplot.polar(r, theta)

The parameter used above are:

  • r: specifies the distance from the origin.
  • theta: Specifies the angle at which distance from the origin has to be measured.

Let’s see an example of plotting error bars in polar coordinates:

# Import Library

import matplotlib.pyplot as plt
import numpy as np
  
# Define Data

r= np.arange(0, 4, 0.5)
theta= 2*np.pi*r

# Define Error

x_error = 0.2
y_error = 5

# Plot polar coordinates

plt.polar(r,theta,"ro")

# Plot error bar

plt.errorbar(r, theta, xerr = x_error, yerr= y_error,                           color='black')    

# Display graph

plt.show()
  • In the above example, we import matplotlib and numpy library.
  • After that, we define ‘r’ and ‘theta‘ i.e distance and angle.
  • Then we plot the polar coordinates by using the plt.polar() method and by using the plt.errorbar() method we plot error bars in the polar plot.
  • Finally, we display plt.show() method to display the polar plot.

You may also like to read the following Matplotlib tutorials.

In this Python tutorial, we have discussed the “Matplotlib plot error bars” and we have also covered some examples related to it. These are the following topics that we have discussed in this tutorial.

  • Matplotlib plot error bars
  • Matplotlib plot error bars example
  • Matplotlib interactive plot error bars
  • Matplotlib chart error bars
  • Matplotlib scatter plot error bars
  • Matplotlib plot_date error bars
  • Matplotlib plot only error bars
  • Matplotlib plot error bars symmetric
  • Matplotlib plot error bars asymmetric
  • Matplotlib polar plot error bars