Pandas 熊猫数据框中的列在pyplot或seaborn中具有特定值时,如何填充颜色垂直区域?

Pandas 熊猫数据框中的列在pyplot或seaborn中具有特定值时,如何填充颜色垂直区域?,pandas,matplotlib,seaborn,python-3.7,Pandas,Matplotlib,Seaborn,Python 3.7,我有一个要绘制的数据帧。它有两列:price和带有datetime索引的alignment。我希望在对齐为-1的位置有黄色的垂直区域。但我不知道怎么做。以下是一个可复制示例的数据样本 df[['Price','Alignment']].to_json() '{"Price":{"1573603200000":-1.6532848145,"1573689600000":-1.664380421,"1573776000000":-1.6665995555,"1574035200000":-1.687

我有一个要绘制的数据帧。它有两列:price和带有datetime索引的alignment。我希望在对齐为-1的位置有黄色的垂直区域。但我不知道怎么做。以下是一个可复制示例的数据样本

df[['Price','Alignment']].to_json()
'{"Price":{"1573603200000":-1.6532848145,"1573689600000":-1.664380421,"1573776000000":-1.6665995555,"1574035200000":-1.6876812211,"1574121600000":-1.6921194636,"1574208000000":-1.6987768407,"1574294400000":-1.6754760407,"1574380800000":-1.6588326178,"1574640000000":-1.6566134965,"1574726400000":-1.6732569194,"1574812800000":-1.6732569194,"1574985600000":-1.6899003423,"1575244800000":-1.6887907817,"1575331200000":-1.6388605129,"1575417600000":-1.664380421,"1575504000000":-1.6788047226,"1575590400000":-1.677695162,"1575849600000":-1.6765856014,"1575936000000":-1.6799142833,"1576022400000":-1.6721473588,"1576108800000":-1.6632708604,"1576195200000":-1.6721473588,"1576454400000":-1.6799142833,"1576540800000":-1.6788047226,"1576627200000":-1.6810238572,"1576713600000":-1.677695162,"1576800000000":-1.6699282375,"1577059200000":-1.6699282375,"1577145600000":-1.6543943752,"1577318400000":-1.5623007489,"1577404800000":-1.5767250638,"1577664000000":-1.624436198,"1577750400000":-1.6410796342,"1577923200000":-1.6499561194,"1578009600000":-1.6155597129,"1578268800000":-1.5922589128,"1578355200000":-1.5966971554,"1578441600000":-1.6166692735,"1578528000000":-1.6144501522,"1578614400000":-1.605573667,"1578873600000":-1.5989162767,"1578960000000":-1.6077927883,"1579046400000":-1.6310935619,"1579132800000":-1.6288744406,"1579219200000":-1.6377509523,"1579564800000":-1.6421891948,"1579651200000":-1.6377509523,"1579737600000":-1.6044641064,"1579824000000":-1.6344222703,"1580083200000":-1.6255457587,"1580169600000":-1.6333127097,"1580256000000":-1.6288744406,"1580342400000":-1.632203149,"1580428800000":-1.6421891948,"1580688000000":-1.6421891948,"1580774400000":-1.6388605129,"1580860800000":-1.62776488,"1580947200000":-1.635531831,"1581033600000":-1.6432987555,"1581292800000":-1.6444083161},"Alignment":{"1573603200000":-1.0,"1573689600000":-1.0,"1573776000000":-1.0,"1574035200000":1.0,"1574121600000":1.0,"1574208000000":1.0,"1574294400000":-1.0,"1574380800000":-1.0,"1574640000000":-1.0,"1574726400000":1.0,"1574812800000":1.0,"1574985600000":1.0,"1575244800000":1.0,"1575331200000":1.0,"1575417600000":1.0,"1575504000000":-1.0,"1575590400000":1.0,"1575849600000":1.0,"1575936000000":1.0,"1576022400000":1.0,"1576108800000":1.0,"1576195200000":1.0,"1576454400000":1.0,"1576540800000":1.0,"1576627200000":1.0,"1576713600000":1.0,"1576800000000":1.0,"1577059200000":1.0,"1577145600000":1.0,"1577318400000":1.0,"1577404800000":1.0,"1577664000000":-1.0,"1577750400000":1.0,"1577923200000":-1.0,"1578009600000":1.0,"1578268800000":1.0,"1578355200000":1.0,"1578441600000":-1.0,"1578528000000":1.0,"1578614400000":1.0,"1578873600000":1.0,"1578960000000":1.0,"1579046400000":1.0,"1579132800000":-1.0,"1579219200000":1.0,"1579564800000":1.0,"1579651200000":1.0,"1579737600000":-1.0,"1579824000000":-1.0,"1580083200000":1.0,"1580169600000":1.0,"1580256000000":1.0,"1580342400000":1.0,"1580428800000":1.0,"1580688000000":1.0,"1580774400000":1.0,"1580860800000":1.0,"1580947200000":1.0,"1581033600000":1.0,"1581292800000":1.0}}'
因此,首先,我会:

df.Price.plot() # That would give me the plot of Price

但是如何在对齐=-1的位置到达垂直线?

使用
ax.axvline

fig, ax = plt.subplots()
df['Price'].plot(ax=ax)
# Plot a vertical yellow line at each date whose Alignment is -1
[ax.axvline(x, c='y') for x in df[df['Alignment'] == -1].index]

使用
ax.axvline

fig, ax = plt.subplots()
df['Price'].plot(ax=ax)
# Plot a vertical yellow line at each date whose Alignment is -1
[ax.axvline(x, c='y') for x in df[df['Alignment'] == -1].index]