【问题标题】:Disabling shading underneath a (discrete) probability mass function in matplotlib在 matplotlib 中禁用(离散)概率质量函数下的阴影
【发布时间】: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 网站上第一张图片中显示的行为。

我的问题:

  1. 为什么在超几何分布中使用的参数很大时会出现这种情况,以及
  2. 如何去除图表下方的蓝色阴影?
  3. 另外,是否可以删除将数据点连接到 x 轴的第一张图片中的“茎”?

注意:我在 VSCode 中使用 Jupyter Notebooks,如果这是一个因素的话。

谢谢。

【问题讨论】:

    标签: python matplotlib scipy


    【解决方案1】:

    您可以减少 n 以使线条像这样可见(这仅取决于 n 这是感兴趣的离散变量(您的示例中的人,scipy 文档中的狗;如果足够大,这将成为一个实心区域):

    [N, K, n] = [2000, 278, 100]
    

    产生

    另外,只需删除产生的ax1.vlines(x, 0, pmf1, lw=2);

    【讨论】:

    • 啊,我想知道教程代码中ax.vlines()的功能。谢谢!
    猜你喜欢
    • 2012-07-29
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2016-01-04
    相关资源
    最近更新 更多