功能

时间:2018-05-24 07:54:44

标签: python matplotlib

我在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;' list'的实例之间不受支持和' int'

我无法找到如何使用where功能

1 个答案:

答案 0 :(得分: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)