Pau Matarrodona Moragas Pau Matarrodona Moragas - 1 month ago 18
Python Question

Different errors with Matplotlib

I want to plot a graph with different errors each point, independent of the value.

My code is here:

import matplotlib.pyplot as plt
x=[1,2,3,4,5,6]
y=[10,20,30,40,50,60]
errorx=[0.1,0.3,0.7,0.6,1,1.5,0.2]
errory=[0.1,0.8,0.4,2.3,0.1,1.2,0.6]
plt.errorbar(x,y,yerr=None,xerr=None)
plt.plot(x,y)


I don't get what I want; I want without the line connecting the dots and the x and y-error bar

Answer

Firstly, you set yerr and xerr equal to None in your example (as seen in your comment), therefore no error bars will be shown. Secondly, your list of x and y errors are not the same length as that of your x and y coordinates. Therefore if you tried to plot this you would get a ValueError: xerr must be a scalar, the same dimensions as x, or 2xN. and the same thing would happen for your the y error too. The errorbar documentation can be found here.

A working example using your data is shown below:

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6]
y = [10,20,30,40,50,60]

errorx = [0.1,0.3,0.7,0.6,1,1.5]  #now the same length as x and y
errory = [0.1,0.8,0.4,2.3,0.1,1.2]

plt.errorbar(x, y, yerr=errory, xerr=errorx, linestyle='') 
#change linestyle to not connect the points

plt.show()

This produces the following graph:

enter image description here