Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use MatPlotLib to plot line graph with error ranges around points

I'm trying to plot a line graph of ten values with error ranges over each point:

u = [1,2,3,4,5,6,7,8,9,10]
plt.errorbar(range(10), u, yerr=1)
plt.show()

I get the error message

ValueError: too many values to unpack

Can someone please advise me the best way to plot a line graph with error bars over each point? http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.errorbar

thx

like image 838
user1452494 Avatar asked Oct 01 '22 08:10

user1452494


1 Answers

plt.errorbar expects the errors to have the same dimension as the x- and y-values (either a list of 2-tuples for up/down errors or a simple list for symmetric errors).

You want to do something like

u = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
plt.errorbar(range(10), u, yerr=[1]*10)

or more clearly with numpy imported as np

u = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
plt.errorbar(np.arange(10), u, yerr=np.ones(10))
like image 81
Benjamin Bannier Avatar answered Oct 10 '22 10:10

Benjamin Bannier