Python的等效于MATLAB的normplot函数是什么?

7

有没有Python中类似于MATLAB的normplot函数的等价函数?也许是在matplotlib中实现的?

MATLAB语法:

x = normrnd(10,1,25,1);
normplot(x)

给出:

enter image description here

我尝试使用matplotlib和numpy模块来确定数组中值的概率/百分位数,但输出图的y轴刻度是线性的,与MATLAB的图相比。

import numpy as np
import matplotlib.pyplot as plt

data =[-11.83,-8.53,-2.86,-6.49,-7.53,-9.74,-9.44,-3.58,-6.68,-13.26,-4.52]
plot_percentiles = range(0, 110, 10) 

x = np.percentile(data, plot_percentiles)
plt.plot(x, plot_percentiles, 'ro-')
plt.xlabel('Value')
plt.ylabel('Probability')  
plt.show() 

给出: 在此输入图片描述 否则,第一个图中的刻度怎么能够调整呢?
谢谢。
4个回答

7
一个晚回答,但是我刚遇到了同样的问题,并找到了一个值得分享的解决方案。我猜。
正如joris所指出的,probplot函数相当于normplot,但所得到的分布形式是累积密度函数。Scipy.stats也提供了一个函数来转换这些值。
累积分布函数 -> 百分位数
stats.'distribution function'.cdf(cdf_value)

百分位数 -> 累计分布函数 (CDF)
stats.'distribution function'.ppf(percentile_value)

例如:

举个例子:

stats.norm.ppf(percentile)

为了得到一个类似于normplot的等价y轴,您可以替换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()

结果如下所示:

使用matplotlib的概率图


2
我相当确定matplotlib没有提供类似的功能。
当然,这是可能的,但您必须重新调整数据并更改您的y轴刻度/标签以匹配,或者如果您计划经常这样做,也可以编写一个新的比例尺,可应用于matplotlib轴,就像在此示例中所示:http://matplotlib.sourceforge.net/examples/api/custom_scale_example.html

2
也许你可以使用Scipy(scipy.stats)的probplot函数,这对我来说似乎相当于MATLAB的normplot:
计算样本数据与指定理论分布的概率图的分位数。 probplot可选地计算数据的最佳拟合线,并使用Matplotlib或给定的绘图函数绘制结果。

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

但这并没有解决您在不同的y轴刻度上的问题。

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接