Python 使用 SciPy 的分位数-分位数图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13865596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Quantile-Quantile Plot using SciPy
提问by John
How would you create a qq-plot using Python?
你将如何使用 Python 创建一个 qq-plot?
Assuming that you have a large set of measurements and are using some plotting function that takes XY-values as input. The function should plot the quantiles of the measurements against the corresponding quantiles of some distribution (normal, uniform...).
假设您有大量测量值,并且正在使用一些以 XY 值作为输入的绘图函数。该函数应根据某些分布(正态、均匀...)的相应分位数绘制测量值的分位数。
The resulting plot lets us then evaluate in our measurement follows the assumed distribution or not.
结果图让我们可以在我们的测量中评估是否遵循假设的分布。
http://en.wikipedia.org/wiki/Quantile-quantile_plot
http://en.wikipedia.org/wiki/Quantile-quantile_plot
Both R and Matlab provide ready made functions for this, but I am wondering what the cleanest method for implementing in in Python would be.
R 和 Matlab 都为此提供了现成的函数,但我想知道在 Python 中实现的最干净的方法是什么。
采纳答案by Geoff
I think that scipy.stats.probplotwill do what you want. See the documentationfor more detail.
我认为这scipy.stats.probplot会做你想做的。有关更多详细信息,请参阅文档。
import numpy as np
import pylab
import scipy.stats as stats
measurements = np.random.normal(loc = 20, scale = 5, size=100)
stats.probplot(measurements, dist="norm", plot=pylab)
pylab.show()
Result
结果


回答by John
I came up with this. Maybe you can improve it. Especially the method of generating the quantiles of the distribution seems cumbersome to me.
我想出了这个。也许你可以改进它。特别是生成分布的分位数的方法对我来说似乎很麻烦。
You could replace np.random.normalwith any other distribution from np.randomto compare data against other distributions.
您可以np.random.normal用任何其他分布替换fromnp.random以将数据与其他分布进行比较。
#!/bin/python
import numpy as np
measurements = np.random.normal(loc = 20, scale = 5, size=100000)
def qq_plot(data, sample_size):
qq = np.ones([sample_size, 2])
np.random.shuffle(data)
qq[:, 0] = np.sort(data[0:sample_size])
qq[:, 1] = np.sort(np.random.normal(size = sample_size))
return qq
print qq_plot(measurements, 1000)
回答by Akavall
Using qqplotof statsmodels.apiis another option:
使用qqplotofstatsmodels.api是另一种选择:
Very basic example:
非常基本的例子:
import numpy as np
import statsmodels.api as sm
import pylab
test = np.random.normal(0,1, 1000)
sm.qqplot(test, line='45')
pylab.show()
Result:
结果:


Documentation and more example are here
文档和更多示例在这里
回答by grasshopper
It exists now in the statsmodels package:
它现在存在于 statsmodels 包中:
http://statsmodels.sourceforge.net/devel/generated/statsmodels.graphics.gofplots.qqplot.html
http://statsmodels.sourceforge.net/devel/generated/statsmodels.graphics.gofplots.qqplot.html
回答by ccap
If you need to do a QQ plot of one sample vs. another, statsmodels includes qqplot_2samples(). Like Ricky Robinson in a comment above, this is what I think of as a QQ plot vs a probability plot which is a sample against a theoretical distribution.
如果您需要绘制一个样本与另一个样本的 QQ 图,statsmodels 包含 qqplot_2samples()。就像上面评论中的 Ricky Robinson 一样,这就是我认为的 QQ 图与概率图,后者是针对理论分布的样本。
回答by sushmit
You can use bokeh
你可以使用散景
from bokeh.plotting import figure, show
from scipy.stats import probplot
# pd_series is the series you want to plot
series1 = probplot(pd_series, dist="norm")
p1 = figure(title="Normal QQ-Plot", background_fill_color="#E8DDCB")
p1.scatter(series1[0][0],series1[0][1], fill_color="red")
show(p1)
回答by Ravi G
import numpy as np
import pylab
import scipy.stats as stats
measurements = np.random.normal(loc = 20, scale = 5, size=100)
stats.probplot(measurements, dist="norm", plot=pylab)
pylab.show()
Here probplot draw the graph measurements vs normal distribution which speofied in dist="norm"
这里 probplot 绘制图形测量值与正态分布的关系,这些分布在 dist="norm" 中

