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
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read