Home > Software design >  Matplotlib errorbar scaled by a factor in plot
Matplotlib errorbar scaled by a factor in plot

Time:04-20

I am plotting data from a CSV file named test.csv that has the values like:

X                      Y            Yerrmin              Yerrmax
13.629119553139 0.13237415706937    0.115879894547257   0.14748971609137
55.3872849395824    0.14385506424916    0.13237415706937    0.153752686711682
208.442201941724    0.144454558978827   0.129650059586552   0.158954011478447
544.589674426085    0.151216201294351   0.13515549098632    0.168483298334671
968.990798410664    0.149341335862913   0.135718731529535   0.164331292710806
1305.01533678836    0.146268074690858   0.13073290613871    0.162970154348788
1596.62602210143    0.14748971609137    0.13292580681435    0.165016119097793

In my code, I have:

testK = np.genfromtxt("test.csv", delimiter=",", names=["X", "Y", "Yerrmin", "Yerrmax"])
testK_yerr=[testK['Yerrmin'], testK['Yerrmax']]
plt.errorbar(testK['X'], testK['Y'], yerr=testK_yerr, fmt='sk')
plt.savefig("test_plot.pdf")

This gives the plot: plot with errorbar

I cannot find the problem why the error bars are scaled by a multiplicative factor

CodePudding user response:

Your Yerrmin values should be the amount less than the data point that you want the error bar to extend down to. So as your first Y point is 0.13 the Y error will extend down to 0.13-0.12 = 0.01, which is where it is. If Yerrmin is actually the minimum Y value, i.e. you want the error band to extend from 0.115 to 0.147 then you need to make make new values to pass to the function like

testK_yerr=[testk['Y']-testK['Yerrmin'], testK['Yerrmax']-testK['Y']]

and pass those instead

  • Related