简体   繁体   中英

Matplotlib semilog plot line of best fit

Below (blue dashed line) is what I get when I try to do linear regression on my data. It looks very off (but maybe it's correct?) Here is the image (does not allow me to embed):

http://i.imgur.com/KiprZDB.png

and here is the code:

mm, cs, err = get_cols(data)
a = np.asarray(mm, dtype=float)
b = np.asarray(cs, dtype=float)
ax.errorbar(a, b, xerr=None, yerr=err, fmt='o', c='b', label='Detection Rate')
logB = np.log10(b)
m, y0 = np.polyfit(a, logB, 1)
ax.plot(a, np.exp(a*m+y0), '--')

The log scale of matplotlib uses the logarithm of base 10 by default. It therefore makes sense to use np.log10(b) to transform the data for fitting.

However, once fitting is done, the data needs to be backtransformed using the inverse of the transformation function.

In case of y = log10(x) the inverse is x = 10**(y) , while
in case of y = log(x) the inverse is x = exp(y) .

So you need to decide for one of the cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM