Errorbar graph in Python using Matplotlib - GeeksforGeeks

14 Jan, 2022
Error bars function used as graphical enhancement that visualizes the variability of the plotted data on a Cartesian graph. Error bars can be applied to graphs to provide an additional layer of detail on the presented data. As you can see in below graphs. 
Error bars help you 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. To visualize this information error bars work by drawing lines that extend from the center of the plotted data point or edge with bar charts the length of an error bar helps to reveal uncertainty of a data point as shown in the below graph. A short error bar shows that values are concentrated signaling that the plotted averaged value is more likely while a long error bar would indicate that the values are more spread out and less reliable. also depending on the type of data. the length of each pair of error bars tends to be of equal length on both sides, however, if the data is skewed then the lengths on each side would be unbalanced.
Error bars always run parallel to a quantity of scale axis so they can be displayed either vertically or horizontally depending on whether the quantitative scale is on the y-axis or x-axis if there are two quantity of scales and two pairs of arrow bars can be used for both axes.  
Let see an example of errorbar how it works.
Creating a Simple Graph.  
Python3
# importing matplotlibimport matplotlib.pyplot as plt # making a simple plotx =[1, 2, 3, 4, 5, 6, 7]y =[1, 2, 1, 2, 1, 2, 1] # plotting graphplt.plot(x, y)
Output: 
Example 1: Adding Some error in y value.  
Python3
# importing matplotlibimport matplotlib.pyplot as plt  # making a simple plotx =[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: 
Example 2: Adding Some error in x value. 
Python3
# importing matplotlibimport matplotlib.pyplot as plt # making a simple plotx =[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: 
Example 3: Adding error in x & y  
Python3
# importing matplotlibimport matplotlib.pyplot as plt  # making a simple plotx =[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: 
Example 4: Adding variable error in x and y  
Python3
# importing matplotlibimport matplotlib.pyplot as plt  # making a simple plotx =[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: 
Example 5:  
Python3
# import require modulesimport numpy as npimport matplotlib.pyplot as plt  # defining our functionx = np.arange(10)/10y = (x + 0.1)**2 # defining our errory_error = np.linspace(0.05, 0.2, 10) # plotting our function and# error barplt.plot(x, y) plt.errorbar(x, y, yerr = y_error, fmt ='o')
Output: 
 
gabaa406
adnanirshad158
Python-matplotlib
Python
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Convert integer to string in Python
Convert string to integer in Python
Python infinity
How to set input type date in dd-mm-yyyy format using HTML ?
Matplotlib.pyplot.title() in Python