Home > Back-end >  How to conditionally shade a datetime span
How to conditionally shade a datetime span

Time:07-08

I want to shade the background of my plot for regions where one column values are negative. Is there a way to do this using axvspan by setting conditions?

df = pd.DataFrame({'date': ["2014-10-04","2014-10-05", "2014-10-06", "2014-10-07", 
                    "2014-10-08", "2014-10-09","2014-10-10"],
                   'values': [0.6, 0.3, 0.1,0.2,0.6,0.3, 0.4],'temp': [4,0,-1,-3,2,-3, -2]})
df['date'] = pd.to_datetime(df['date'], format = '%Y-%m-%d')
df.set_index('date', inplace = True)

            values  temp
date                    
2014-10-04     0.6     4
2014-10-05     0.3     0
2014-10-06     0.1    -1
2014-10-07     0.2    -3
2014-10-08     0.6     2
2014-10-09     0.3    -3
2014-10-10     0.4    -2

With a plot similar to this, but the shaded regions where the 'temp' column numbers are < 0. The real data is a very long time series and I cannot easily tell where xmin and xmax would begin and end.

pl.plot(df['values'])
pl.axvspan("2014-10-04", "2014-10-05", color = 'lightblue')

enter image description here

CodePudding user response:

  • Plot the dataframe directly with enter image description here

  • Related