【问题标题】:Fill_Between returns ValueError: Argument dimensions are incompatibleFill_Between 返回 ValueError:参数维度不兼容
【发布时间】:2015-09-03 00:20:49
【问题描述】:

我想在 x=0 到 x ValueError: Argument dimensions are incompatible。我做错了什么?

import matplotlib.pyplot as plt

fig, (ax1) = plt.subplots(1,1)
x = [1,2,3]
y=[10,20,30]
ax1.plot(x, y)
ax1.fill_between(x, 0, y, where=x<3, facecolor='green', interpolate=True)
plt.show()

【问题讨论】:

    标签: python python-2.7 matplotlib


    【解决方案1】:

    问题是尝试使用标准 python 列表创建布尔掩码。如果您将xy 转换为numpy.array,那么一切都应该有效。

    import matplotlib.pyplot as plt
    import numpy as np
    
    fig, (ax1) = plt.subplots(1)
    x = np.array([1,2,3])
    y = np.array([10,20,30])
    ax1.plot(x, y)
    ax1.fill_between(x, 0, y, where=x<3, facecolor='green', interpolate=True)
    plt.show()
    

    如果x 是一个python 列表,x&lt;3 返回False,但fill_betweenwhere 关键字需要N-length numpy 布尔数组。

    In [89]: x=[1,2,3]
    
    In [91]: x<3
    Out[91]: False
    
    In [92]: x=np.array(x)
    
    In [93]: x<3
    Out[93]: array([ True,  True, False], dtype=bool)
    

    【讨论】:

    • 可能值得注意的是,这是一个 python 遗留问题 (2),如 py3 [1, 2, 3] &lt; 3 引发 TypeErrorFalse 结果是由于 python 回退到比较对象 id(iirc,可能是内存地址)作为最后的手段,所以我不确定这是否是确定性的。
    猜你喜欢
    • 2013-08-21
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2021-05-29
    • 2016-08-26
    • 2014-04-12
    相关资源
    最近更新 更多