首页 > 解决方案 > 如何在 df=5 和 alpha=0.05 的 jupyter notebook 中绘制卡方图?

问题描述

如何在 jupyter notebook 中绘制卡方图?

它是双尾的,alpha = 0.05 和 df = 5

我只想绘制它并显示关键区域

标签: pythonmatplotlibscipy

解决方案


使用.interval方法获取置信区间

In [1]: import scipy.stats as ss

In [2]: import matplotlib.pyplot as plt

In [3]: import numpy as np

# Distrubution object, chisqure with df=5
In [4]: dist = ss.chi2(5)

# Two tail C.I.
In [5]: dist.interval(0.95)
Out[5]: (0.831211613486663, 12.832501994030027)

In [7]: plt.plot(np.linspace(0,20), dist.pdf(np.linspace(0,20)))
Out[7]: [<matplotlib.lines.Line2D at 0x1a0e9fe048>]

In [10]: plt.vlines(dist.interval(0.95), ymin=0, ymax=plt.ylim()[1])
Out[10]: <matplotlib.collections.LineCollection at 0x1a16df7940>

In [11]: plt.show()

在此处输入图像描述


推荐阅读