【问题标题】:matplotlib fill_between issue with x argumentmatplotlib fill_between x 参数问题
【发布时间】:2013-05-12 01:45:54
【问题描述】:

我尝试在我的绘图下方填充一个空间,绘图是 y=x,所以它是一条 45 度角的直线。我尝试将曲线下方的区域从 x=1 填充到 x=10,如何使用 fill_between 来填充?

【问题讨论】:

    标签: matplotlib


    【解决方案1】:

    这就是 where 关键字参数的用途。

    在哪里: 如果没有,默认填充在任何地方。如果不是 None,它是一个 N 长度的 numpy 布尔数组,填充只会发生在 where==True 的区域。

    例如:

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots()
    >>> x = np.linspace(0, 10, 50)
    >>> y = x**2
    >>> ax.plot(x, y, 'r-')
    [<matplotlib.lines.Line2D object at 0x1e91250>]
    >>> wh = (x>1) & (x<10)    
    >>> ax.fill_between(x, y, where=wh, alpha=0.2)
    <matplotlib.collections.PolyCollection object at 0x24dd210>
    >>> plt.show()
    

    【讨论】:

    • 就我而言,我使用了列表,但这并没有什么区别。我得到一个错误: raise ValueError("Argument dimensions are incompatible") x 和 y 是相同的维度,我可以看到情节:/
    • @nykon 实际上,对变量 x 使用列表会有所不同。当使用wh = (x&gt;1) &amp; (x&lt;10) 其中x 是numpy.ndarray 时,wh 被分配numpy.ndarray 的bools。当x 是list 时,wh 被分配一个bool。正如您在@Zhenya 提供的答案中看到的那样,where 仅将None 或布尔值numpy.ndarray 作为参数;将bool 作为参数传递给ValueError。
    • 是的,这就是我没有考虑这方面的一点。谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2018-06-20
    • 1970-01-01
    相关资源
    最近更新 更多