【发布时间】:2021-01-06 09:39:28
【问题描述】:
我正在尝试使用以下代码为离散随机变量 X 绘制超几何概率质量函数:
# Hypergeometric Distribution code adapted from https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.hypergeom.html
import numpy as np
from scipy.stats import hypergeom, norm
import matplotlib.pyplot as plt
# (N, K, n) The hypergeometric distribution models drawing objects from a bin. N is the total number of objects, K is total number of Type I objects. The random variate represents the number of Type I objects in n drawn without replacement from the total population.
[N, K, n] = [2000, 278, 500]
rv1 = hypergeom(N, K, n)
x = np.arange(0, n+1)
pmf1 = rv1.pmf(x)
xlabel="Number of people who are interested"; ylabel="Probability of event (PMF)"
fig = plt.figure(figsize=(8,7))
ax1 = fig.add_subplot(211)
ax1.plot(x, pmf1, 'bo:', aa=True)
ax1.vlines(x, 0, pmf1, lw=2); ax1.set_xlabel(xlabel); ax1.set_ylabel(ylabel)
根据Scipy docs 的原始代码,我希望它会生成一个茎图,如下他们网站的图片所示:
当我使用 ax1.set_xlim(left=None, right=200) 减小 x 轴的范围时,它会显示相同的行为:图表下方的蓝色阴影。但是,如果我将分布参数从 [N, K, n] = [2000, 278, 500] 减少到 [200, 30, 50],它会恢复到 Matplotlib 网站上第一张图片中显示的行为。
我的问题:
- 为什么在超几何分布中使用的参数很大时会出现这种情况,以及
- 如何去除图表下方的蓝色阴影?
- 另外,是否可以删除将数据点连接到 x 轴的第一张图片中的“茎”?
注意:我在 VSCode 中使用 Jupyter Notebooks,如果这是一个因素的话。
谢谢。
【问题讨论】:
标签: python matplotlib scipy