简体   繁体   English

Matplotlib fill_between

[英]Matplotlib fill_between

I'm trying to use fill_between with a pandas Series values but it's not working. 我正在尝试将fill_between与pandas Series值一起使用,但是它不起作用。 The 'DAY' field is string date format like '%Y-%m-%d' . 'DAY'字段是字符串日期格式,例如'%Y-%m-%d'

df_tmp like: df_tmp像:

MEDIA_B2W  MEDIA_CONC  UPPER_BOUND  LOWER_BOUND     DAY
2017.48     2512.55       2811.0       1924.0       2017-01-01
1999.38     2512.55       2811.0       1924.0       2017-01-02
1930.89     2512.55       2811.0       1924.0       2017-01-03

df_tmp[['UPPER_BOUND','LOWER_BOUND','MEDIA_CONC','MEDIA_B2W','DAY']].plot(
                 x='DAY',ax=ax[0],grid=True,style=['r-','b-','y--','g-o'])

ax[0].fill_between(df_tmp.index,df_tmp['UPPER_BOUND'], df_tmp['LOWER_BOUND'],
                    facecolor='green', alpha=0.2, interpolate=True)

I would like to color between the upper and lower bound. 我想在上下限之间进行着色。 This is the current plot 这是当前情节

Just the lines appear in the plot. 仅线条出现在绘图中。

This workaround uses df indices for x ticks, and then swaps for the time series. 此解决方法将df索引用于x刻度,然后将其交换为时间序列。

df = df[['UPPER_BOUND','LOWER_BOUND','MEDIA_CONC','MEDIA_B2W','DAY']]

ax = df.plot(x=df.index, grid=True, style=['r-','b-','y--','g-o'])
ax.fill_between(df.index, df.LOWER_BOUND, df.UPPER_BOUND,
                facecolor='green', alpha=0.2, interpolate=True)

# replace index values with dates
ax.set_xticks(df.index)
ax.set_xticklabels(df.DAY)

# cosmetic adjustments
pad = 700
ax.set_ylim([df.LOWER_BOUND.min()-pad, df.UPPER_BOUND.max()+pad])

时间序列之间的填充

Alternately, you can set DAY as df.index : 或者,您可以将DAY设置为df.index

df.DAY = pd.to_datetime(df.DAY)
df = df.set_index('DAY')

ax = df.plot(grid=True, style=['r-','b-','y--','g-o'])
ax.fill_between(df.index, df.LOWER_BOUND, df.UPPER_BOUND,
                facecolor='green', alpha=0.2, interpolate=True)

# cosmetic adjustments
pad = 700
_=ax.set_ylim([df.LOWER_BOUND.min()-pad, df.UPPER_BOUND.max()+pad])

介于2之间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM