Matplotlib plt.fill_between'isinfinite'错误

时间:2018-06-23 11:04:12

标签: matplotlib

我有此代码:

g1 = plt.plot(centers[0],'-',label=u'Group 1',color='#1f77b4')
g1_max = plt.plot(centers[0]+max_g1,'-',color='#1f77b4',alpha=0.5)
plt.fill_between(range(len(g1)),g1,g1_max,facecolor='#1f77b4',alpha=0.5)

然后出现以下错误:

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

任何线索为何? 谢谢

1 个答案:

答案 0 :(得分:0)

plt.fill_between在这里使用不正确,x,y1和y2必须是列表,而不是生成器或绘图对象(请参见https://matplotlib.org/api/_as_gen/matplotlib.pyplot.fill_between.html)。 可能以下方法会起作用:

import numpy as np

g1 = plt.plot(centers[0],'-',label=u'Group 1',color='#1f77b4')
g1_max = plt.plot(centers[0]+max_g1,'-',color='#1f77b4',alpha=0.5)
plt.fill_between(np.arange(len(g1)),centers[0],centers[0]+g1_max,facecolor='#1f77b4',alpha=0.5)