我有一个数据框,其中包含一段时间内的价格信息。我还使用布尔值将某些时期标记为上升或下降。我想绘制一段时间内的价格,并在向上== True表示绿色,而向下== True表示红色的区域阴影。我正在努力想办法。
编辑:我正在尝试使用matplotlib / seaborn在一段时间的线图上绘制价格,并希望为“ True”区域加阴影。我尝试使用ax.axvspan进行此操作,但不确定如何为x传递正确的索引。
有什么想法吗?
price upturn downturn
2016-12-31 954.623021 False True
2017-01-01 973.662396 False True
2017-01-02 1011.492500 False True
2017-01-03 1020.493750 True False
2017-01-04 1076.784792 True False
2017-01-05 1051.258854 True False
2017-01-06 931.354688 True False
2017-01-07 865.056667 False True
2017-01-08 908.179063 False True
2017-01-09 891.121979 False True
2017-01-10 900.545208 False True
2017-01-11 845.028437 False True
2017-01-12 780.695313 False True
2017-01-13 805.582187 False False
2017-01-14 827.220625 False False
这里的数据框易于使用:
import pandas as pd
from pandas import Timestamp
df = pd.DataFrame({'price': {Timestamp('2016-12-31 00:00:00'): 954.6230208333336,
Timestamp('2017-01-01 00:00:00'): 973.6623958333333,
Timestamp('2017-01-02 00:00:00'): 1011.4925000000002,
Timestamp('2017-01-03 00:00:00'): 1020.4937500000001,
Timestamp('2017-01-04 00:00:00'): 1076.784791666667,
Timestamp('2017-01-05 00:00:00'): 1051.2588541666669,
Timestamp('2017-01-06 00:00:00'): 931.3546875000002,
Timestamp('2017-01-07 00:00:00'): 865.0566666666665,
Timestamp('2017-01-08 00:00:00'): 908.1790625000002,
Timestamp('2017-01-09 00:00:00'): 891.1219791666667,
Timestamp('2017-01-10 00:00:00'): 900.5452083333333,
Timestamp('2017-01-11 00:00:00'): 845.0284375,
Timestamp('2017-01-12 00:00:00'): 780.6953125000001,
Timestamp('2017-01-13 00:00:00'): 805.5821874999998,
Timestamp('2017-01-14 00:00:00'): 827.2206249999999},
'upturn': {Timestamp('2016-12-31 00:00:00'): False,
Timestamp('2017-01-01 00:00:00'): False,
Timestamp('2017-01-02 00:00:00'): False,
Timestamp('2017-01-03 00:00:00'): True,
Timestamp('2017-01-04 00:00:00'): True,
Timestamp('2017-01-05 00:00:00'): True,
Timestamp('2017-01-06 00:00:00'): True,
Timestamp('2017-01-07 00:00:00'): False,
Timestamp('2017-01-08 00:00:00'): False,
Timestamp('2017-01-09 00:00:00'): False,
Timestamp('2017-01-10 00:00:00'): False,
Timestamp('2017-01-11 00:00:00'): False,
Timestamp('2017-01-12 00:00:00'): False,
Timestamp('2017-01-13 00:00:00'): False,
Timestamp('2017-01-14 00:00:00'): False},
'downturn': {Timestamp('2016-12-31 00:00:00'): True,
Timestamp('2017-01-01 00:00:00'): True,
Timestamp('2017-01-02 00:00:00'): True,
Timestamp('2017-01-03 00:00:00'): False,
Timestamp('2017-01-04 00:00:00'): False,
Timestamp('2017-01-05 00:00:00'): False,
Timestamp('2017-01-06 00:00:00'): False,
Timestamp('2017-01-07 00:00:00'): True,
Timestamp('2017-01-08 00:00:00'): True,
Timestamp('2017-01-09 00:00:00'): True,
Timestamp('2017-01-10 00:00:00'): True,
Timestamp('2017-01-11 00:00:00'): True,
Timestamp('2017-01-12 00:00:00'): True,
Timestamp('2017-01-13 00:00:00'): False,
Timestamp('2017-01-14 00:00:00'): False}})
所需输出示例:
答案 0 :(得分:0)
我从评论中的答案中得到了提示,并设置了背景。这是一个手动设置,因此,如果您可以使用上转和下转来确定背景颜色,那就太好了,但这是当前的答案。
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
fig, ax = plt.subplots(figsize=(16,5))
ax.plot(df.index, df['price'])
s_date = mdates.date2num(df.index[0])
e_date = mdates.date2num(df.index[2])
ax.axvspan(s_date, e_date, color='g', alpha=0.4)
s_date1 = mdates.date2num(df.index[2])
e_date1 = mdates.date2num(df.index[6])
ax.axvspan(s_date1, e_date1, color='r', alpha=0.4)
s_date2 = mdates.date2num(df.index[6])
e_date2 = mdates.date2num(df.index[11])
ax.axvspan(s_date2, e_date2, color='g', alpha=0.4)
plt.show()