Movatterモバイル変換


[0]ホーム

URL:


Open In App

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:

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.

Errorbar graph in Python using Matplotlib

Types of Error Bars

Error bars can be applied in two main orientations:

  1. Vertical Error Bars: Applied when the uncertainty is along the y-axis (dependent variable).
  2. 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.

Errorbar graph in Python using Matplotlib

Visualizing Error Bars: Examples

Let see an example of error bar how it works.

Creating a Simple Graph

Python
importmatplotlib.pyplotaspltx=[1,2,3,4,5,6,7]y=[1,2,1,2,1,2,1]plt.plot(x,y)

Output

Errorbar graph in Python using Matplotlib

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
importmatplotlib.pyplotaspltx=[1,2,3,4,5,6,7]y=[1,2,1,2,1,2,1]# creating errory_error=0.2# plotting graphplt.plot(x,y)plt.errorbar(x,y,yerr=y_error,fmt='o')

Output: 

Errorbar graph in Python using Matplotlib

Example 2: Adding Error to the x-values

Here, error bars are applied to the x-axis, indicating uncertainty in the independent variable.

Python
importmatplotlib.pyplotaspltx=[1,2,3,4,5,6,7]y=[1,2,1,2,1,2,1]# creating errorx_error=0.5# plotting graphplt.plot(x,y)plt.errorbar(x,y,xerr=x_error,fmt='o')

Output

Errorbar graph in Python using Matplotlib

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
importmatplotlib.pyplotaspltx=[1,2,3,4,5,6,7]y=[1,2,1,2,1,2,1]# creating errorx_error=0.5y_error=0.3# plotting graphplt.plot(x,y)plt.errorbar(x,y,yerr=y_error,xerr=x_error,fmt='o')

Output

Errorbar graph in Python using Matplotlib

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
importmatplotlib.pyplotaspltx=[1,2,3,4,5]y=[1,2,1,2,1]# creating errory_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.5y_error=[y_errormin,y_errormax]# plotting graph# plt.plot(x, y)plt.errorbar(x,y,yerr=y_error,xerr=x_error,fmt='o')

Output:

Errorbar graph in Python using Matplotlib

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
importnumpyasnpimportmatplotlib.pyplotasplt# defining our functionx=np.arange(10)/10y=(x+0.1)**2# defining our errory_error=np.linspace(0.05,0.2,10)# error barplt.plot(x,y)plt.errorbar(x,y,yerr=y_error,fmt='o')

Output 

Errorbar graph in Python using Matplotlib


Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp