【问题标题】:Matplotlib fill between functionalityMatplotlib 在功能之间填充
【发布时间】:2018-11-03 08:41:06
【问题描述】:

我在 matplotlib 上的 sentdex 教程中看到了 fill_between 的使用。 代码就像

import matplotlib.pyplot as plt

axis=plt.subplot2grid((1,1),(0,0)) #a and b are lists of equal sizes
axis.plot(a,b)
axis.fill_between(a,b,34,where=(b>34),facecolor='r',alpha=.5)

现在他的代码运行良好,他在输出中得到的是 b 中值大于 34 的部分的彩色面部。 但同样给我带来了错误:

TypeError: '>' 在 'list' 和 'int' 的实例之间不支持

我找不到如何使用 where 功能

【问题讨论】:

  • But the same gives error for me : TypeError: '>' not supported between instances of 'list' and 'int'当你写什么不同的代码时,会抛出这个错误呢?
  • 请创建一个minimal reproducible example。在任何情况下,本教程可能一直在使用numpy 数组,不会出现此错误
  • DavidG 似乎打对了。 OP 的代码有一个注释(#a 和 b 是列表...)如果 b 真的是一个列表而不是一个 numpy 数组,则错误告诉您确切的问题:在“列表”和'int'。
  • 对不起我的错误。这是一个 numpy 数组,现在工作正常。

标签: python matplotlib


【解决方案1】:

所以,我正在学习相同的教程,但遇到了同样的问题。您需要导入 numpy 库,然后将您的列表转换为 numpy 数组,然后一切都会正常运行。

import matplotlib.pyplot as plt
import numpy

b = numpy.array(b)

axis=plt.subplot2grid((1,1),(0,0)) #a and b are lists of equal sizes
axis.plot(a,b)
axis.fill_between(a,b,34,where=(b>34),facecolor='r',alpha=.5)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 2017-06-04
    • 2015-12-29
    • 1970-01-01
    相关资源
    最近更新 更多