Python 相当于 MATLAB 的范数图?

发布于 2024-11-16 09:16:09 字数 769 浏览 2 评论 0原文

是否有类似于 MATLAB 中的 normplot 的 python 等效函数? 也许在 matplotlib 中?

MATLAB 语法:

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

给出:

在此处输入图像描述

我尝试过使用 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() 

给出: 在此处输入图像描述

否则,如何像第一个图中那样调整比例?

谢谢。

Is there a python equivalent function similar to normplot from MATLAB?
Perhaps in matplotlib?

MATLAB syntax:

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

Gives:

enter image description here

I have tried using matplotlib & numpy module to determine the probability/percentile of the values in array but the output plot y-axis scales are linear as compared to the plot from 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() 

Gives:
enter image description here

Else, how could the scales be adjusted as in the first plot?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

多情出卖 2024-11-23 09:16:09

答案很晚,但我刚刚遇到了同样的问题并找到了解决方案,值得分享。我猜。

正如乔里斯指出的那样,概率图函数与范数图等效,但所得分布采用累积密度函数的形式。 Scipy.stats 还提供了一个函数来转换这些值。

cdf->百分位

stats.'distribution function'.cdf(cdf_value)

百分位 -> cdf

stats.'distribution function'.ppf(percentile_value)

例如

stats.norm.ppf(percentile)

:要获得等效的 y 轴,如normplot,您可以替换 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

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:

probability plot with matplotlib

离旧人 2024-11-23 09:16:09

我相当确定 matplotlib 不提供这样的东西。

当然,这是可能的,但是您必须重新调整数据并更改 y 轴刻度/标签以匹配,或者,如果您打算经常这样做,也许可以编写一个可以应用的新比例到 matplotlib 轴,如本例所示: http://matplotlib.sourceforge.net/examples/api/custom_scale_example.html

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.

等风也等你 2024-11-23 09:16:09

也许你可以使用 scipy 的 probplot 函数 (scipy.stats),在我看来,这相当于 MATLAB 标准图:

计算概率的分位数
样本数据相对于
指定的理论分布。

probplot 可选择计算
数据和绘图的最佳拟合线
使用 Matplotlib 或 a 的结果
给定绘图函数。

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

但这并不能解决不同 y 轴刻度的问题。

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.

我是男神闪亮亮 2024-11-23 09:16:09

使用 matplotlib.semilogy 将得到更接近matlab的输出。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文