varantir
varantir

Reputation: 6854

Errorbars with logscale in matplotlib

Let us assume I have data x with an error Sx which I want to plot with the method errorbar. Now I am wondering what happens if I rescale to logarithmic scale, does it do the error correctly? The error propagation should go

f(x) = log(x)

=> Sf = |Sx / x|

I could imagine that matplotlib just does

Sf = log Sx

which would be totally wrong. So, what is matplotlib actually doing?

Upvotes: 1

Views: 5360

Answers (2)

ch.w.
ch.w.

Reputation: 21

Indeed, it puzzles me as well. Imagine that I have a file contains a set of data:

xi, yi(xi), sigma(yi) ; i=1,2,....,N

where sigma(yi) is the one standard error of yi(xi). Now, suppose I plot this data using matplotlib, where both x-scale and y-scale are linear. Certainly, the marks on the y axis will be one at yi(xi) - sigma(yi) and another at yi(xi) + sigma(yi). The difference of them is sigma(yi).

The question is, if I set

ax.set_yscale("log")

then, will I see the marks on the log10(y) axis being one at log10( yi(xi)-sigma(yi) ) and another at log10( yi(xi)+sigma(yi) ) ?

However, the above error is not true, since the error of log10(yi(xi)) is certainly not simply as log10( sigma(yi)), instead, error propagation has to be made, via

sigma( log10(yi) )= log10(e) * | sigma(yi)/yi |

So, does anyone know, will error propagation be done while plotting the data with yerrorbars in log y scale?

Upvotes: 2

tacaswell
tacaswell

Reputation: 87376

The way errorbar works is (more-or-less) at each point where you want an errobar drawn it puts a mark at y + err_p and y - err_n in data coordinates. The log scale is applied as part of the transformation from data space -> screen space.

This is rather unambiguously the right thing for a plotting library to do. What you seem to want is propagate error through some computations which requires knowing what the computations are (so you can get all the partials) and is not the business mpl is in. Maybe take a look at sympy.

Upvotes: 2

Related Questions