Python equivalent for MATLAB's normplot?

牧云@^-^@ 提交于 2019-12-03 03:21:01

A late answer, but I just came across the same problem and found a solution, that is worth sharing. I guess.

As joris pointed out the probplot function is an equivalent to normplot, but the resulting distribution is in form of the cumulative density function. Scipy.stats also offers a function, to convert these values.

cdf -> percentile

stats.'distribution function'.cdf(cdf_value)

percentile -> cdf

stats.'distribution function'.ppf(percentile_value)

for example:

stats.norm.ppf(percentile)

To get an equivalent y-axis, like normplot, you can replace the cdf-ticks:

from scipy import stats
import matplotlib.pyplot as plt

nsample=500

#create list of random variables
x=stats.t.rvs(100, size=nsample)

# Calculate quantiles and least-square-fit curve
(quantiles, values), (slope, intercept, r) = stats.probplot(x, dist='norm')

#plot results
plt.plot(values, quantiles,'ob')
plt.plot(quantiles * slope + intercept, quantiles, 'r')

#define ticks
ticks_perc=[1, 5, 10, 20, 50, 80, 90, 95, 99]

#transfrom them from precentile to cumulative density
ticks_quan=[stats.norm.ppf(i/100.) for i in ticks_perc]

#assign new ticks
plt.yticks(ticks_quan,ticks_perc)

#show plot
plt.grid()
plt.show()

The result:

I'm fairly certain matplotlib doesn't provide anything like this.

It's possible to do, of course, but you'll have to either rescale your data and change your y axis ticks/labels to match, or, if you're planning on doing this often, perhaps code a new scale that can be applied to matplotlib axes, like in this example: http://matplotlib.sourceforge.net/examples/api/custom_scale_example.html.

Maybe you can use the probplot function of scipy (scipy.stats), this seems to me an equivalent for MATLABs normplot:

Calculate quantiles for a probability plot of sample data against a specified theoretical distribution.

probplot optionally calculates a best-fit line for the data and plots the results using Matplotlib or a given plot function.

http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.probplot.html

But is does not solve your problem of the different y-axis scale.

Using matplotlib.semilogy will get closer to the matlab output.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!