Python Matplotlib plot df,如果条件允许,在索引值之间填充

Python Matplotlib plot df,如果条件允许,在索引值之间填充,python,pandas,matplotlib,conditional-statements,fill,Python,Pandas,Matplotlib,Conditional Statements,Fill,我有一个问题: ro laws ro ordos January 468 579 February 216 231 March 1005 276 April 1044 250 May 962 276 June 999 568 Jul

我有一个问题:

           ro laws  ro ordos  
January        468       579     
February       216       231     
March         1005       276      
April         1044       250     
May            962       276       
June           999       568      
July          1360       298      
August          56       934      
September      202       289       
October        901       324       
November      1056       412       
December      1121       525
我使用matplotlib绘制它,它按预期工作。我想在这两行之间填充,但仅当x轴为
'一月'
'二月'
'三月'

我试过:

ax.fill_between(plotMonths.index, plotMonths['ro laws'], plotMonths['ro ordos'], where=(plotMonths.index in ['January', "February", 'March']),
                       facecolor='lightskyblue', 
                       alpha=0.2)
d = plotMonths.index.values.tolist() 
ax.fill_between(d, plotMonths['ro laws'], plotMonths['ro ordos'], where=(d in ['January', "February", 'March']),
                       facecolor='lightskyblue', 
                       alpha=0.2)
但它抛出了一个错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我试过:

ax.fill_between(plotMonths.index, plotMonths['ro laws'], plotMonths['ro ordos'], where=(plotMonths.index in ['January', "February", 'March']),
                       facecolor='lightskyblue', 
                       alpha=0.2)
d = plotMonths.index.values.tolist() 
ax.fill_between(d, plotMonths['ro laws'], plotMonths['ro ordos'], where=(d in ['January', "February", 'March']),
                       facecolor='lightskyblue', 
                       alpha=0.2)
但它什么也没做,情节没有被填满

我怎样才能解决这个问题

多谢各位。

Python
in
关键字检查iterable中的元素,但您使用它检查数组(
plotMonths.index
)是否在iterable
['一月','二月','三月']

将条件向量化的正确方法是使用内置方法。
因此,您应该使用:

ax.fill_between(x = plotMonths.index,
                y1 = plotMonths['ro laws'],
                y2 = plotMonths['ro ordos'],
                where = plotMonths.index.isin(['January', "February", 'March']),
                facecolor = 'lightskyblue',
                alpha = 0.2)

Python
in
关键字检查iterable中的元素,但您使用它检查数组(
plotMonths.index
)是否在iterable
['一月','二月','三月']

将条件向量化的正确方法是使用内置方法。
因此,您应该使用:

ax.fill_between(x = plotMonths.index,
                y1 = plotMonths['ro laws'],
                y2 = plotMonths['ro ordos'],
                where = plotMonths.index.isin(['January', "February", 'March']),
                facecolor = 'lightskyblue',
                alpha = 0.2)

只需对数据进行切片即可。例如,前三个月=[0:3],如下所示:

import matplotlib.pyplot as plt
ax.plot(plotMonths.index, plotMonths['ro laws'])
ax.plot(plotMonths.index, plotMonths['ro ordos'])
ax.fill_between(plotMonths.index[0:3], plotMonths['ro laws'][0:3])
ax.fill_between(plotMonths.index[0:3], plotMonths['ro ordos'][0:3])
plt.show()
它将只填充你想要的子集。
下面是一个关于结果的示例。

只需对数据进行切片即可。例如,前三个月=[0:3],如下所示:

import matplotlib.pyplot as plt
ax.plot(plotMonths.index, plotMonths['ro laws'])
ax.plot(plotMonths.index, plotMonths['ro ordos'])
ax.fill_between(plotMonths.index[0:3], plotMonths['ro laws'][0:3])
ax.fill_between(plotMonths.index[0:3], plotMonths['ro ordos'][0:3])
plt.show()
它将只填充你想要的子集。 下面是一个关于结果的例子