Errorbar graph in Python using Matplotlib
Last Updated :
11 Apr, 2025
Error bars are a graphical overlay used to display the variability or uncertainty of points plotted on a Cartesian graph. They provide a further level of information to data shown, giving an indication of the accuracy of measurements and making a more accurate representation of variability in the data. They are drawn as lines that extend from the center of a data point, either vertically or horizontally, depending on the axis. The length of an error bar indicates how precise the measurement is:
- Short error bars indicate that the values are tightly clustered around the data point, suggesting high reliability.
- Long error bars indicate more spread-out values, signaling lower precision and greater uncertainty.
In most cases, the length of the error bars is the same on both sides of the data point. However, if the data distribution is skewed, the lengths of the error bars may differ.

Types of Error Bars
Error bars can be applied in two main orientations:
- Vertical Error Bars: Applied when the uncertainty is along the y-axis (dependent variable).
- Horizontal Error Bars: Used when the uncertainty lies along the x-axis (independent variable).
If both axes have uncertainty, error bars can be applied to both axes simultaneously.

Visualizing Error Bars: Examples
Let see an example of error bar how it works.
Creating a Simple Graph
Python
import matplotlib.pyplot as plt
x =[1, 2, 3, 4, 5, 6, 7]
y =[1, 2, 1, 2, 1, 2, 1]
plt.plot(x, y)
Output

Example 1: Adding Error to the y-values
This example demonstrates how to apply error bars to the y-axis, showing the uncertainty in the dependent variable.Â
Python
import matplotlib.pyplot as plt
x =[1, 2, 3, 4, 5, 6, 7]
y =[1, 2, 1, 2, 1, 2, 1]
# creating error
y_error = 0.2
# plotting graph
plt.plot(x, y)
plt.errorbar(x, y,
yerr = y_error,
fmt ='o')
Output:Â

Example 2: Adding Error to the x-values
Here, error bars are applied to the x-axis, indicating uncertainty in the independent variable.
Python
import matplotlib.pyplot as plt
x =[1, 2, 3, 4, 5, 6, 7]
y =[1, 2, 1, 2, 1, 2, 1]
# creating error
x_error = 0.5
# plotting graph
plt.plot(x, y)
plt.errorbar(x, y,
xerr = x_error,
fmt ='o')
Output

Example 3: Adding Error to Both x and y
This example shows how to apply error bars to both axes simultaneously, giving a more complete view of the data's variability.Â
Python
import matplotlib.pyplot as plt
x =[1, 2, 3, 4, 5, 6, 7]
y =[1, 2, 1, 2, 1, 2, 1]
# creating error
x_error = 0.5
y_error = 0.3
# plotting graph
plt.plot(x, y)
plt.errorbar(x, y,
yerr = y_error,
xerr = x_error,
fmt ='o')
Output

Example 4: Variable Error in x and y
This demonstrates how error bars can vary in length depending on the data, reflecting different levels of uncertainty for each data point.
Python
import matplotlib.pyplot as plt
x =[1, 2, 3, 4, 5]
y =[1, 2, 1, 2, 1]
# creating error
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]
# plotting graph
# plt.plot(x, y)
plt.errorbar(x, y,
yerr = y_error,
xerr = x_error,
fmt ='o')
Output:

Example 5
A more complex example, illustrating how error bars can be used in different contexts to represent data with varying degrees of precision.
Python
import numpy as np
import matplotlib.pyplot as plt
# defining our function
x = np.arange(10)/10
y = (x + 0.1)**2
# defining our error
y_error = np.linspace(0.05, 0.2, 10)
# error bar
plt.plot(x, y)
plt.errorbar(x, y, yerr = y_error, fmt ='o')
OutputÂ
Similar Reads
Matplotlib.pyplot.errorbar() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.errorbar() Function: The errorbar() function in pyplot module of matplotlib library is
3 min read
Matplotlib.axes.Axes.errorbar() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
3 min read
Python | Visualize graphs generated in NetworkX using Matplotlib Prerequisites: Generating Graph using Network X, Matplotlib IntroIn this article, we will be discussing how to plot a graph generated by NetworkX in Python using Matplotlib. NetworkX is not a graph visualizing package but basic drawing with Matplotlib is included in the software package. Step 1 : Im
3 min read
Matplotlib.axes.Axes.semilogy() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library.The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute. m
2 min read
Plotting Sine and Cosine Graph using Matplotlib in Python Data visualization and Plotting is an essential skill that allows us to spot trends in data and outliers. With the help of plots, we can easily discover and present useful information about the data. In this article, we are going to plot a sine and cosine graph using Matplotlib in Python. Matplotlib
3 min read
How to plot Bar Graph in Python using CSV file? CSV stands for "comma separated values", that means the values are distinguished by putting commas and newline characters. A CSV file provides a table like format that can be read by almost every spreadsheet reader like Microsoft Excel and Google Spreadsheet. A Bar Graph uses labels and values where
2 min read
How to animate 3D Graph using Matplotlib? Prerequisites: Matplotlib, NumPy Graphical representations are always easy to understand and are adopted and preferable before any written or verbal communication. With Matplotlib we can draw different types of Graphical data. In this article, we will try to understand, How can we create a beautiful
4 min read
Graph Plotting in Python | Set 1 This series will introduce you to graphing in Python with Matplotlib, which is arguably the most popular graphing and data visualization library for Python.InstallationThe easiest way to install matplotlib is to use pip. Type the following command in the terminal:Â pip install matplotlibOR, you can d
9 min read
Matplotlib.figure.Figure.show() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
2 min read
Matplotlib.figure.Figure.draw() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
2 min read