Home > database >  seaborn lineplot time-series indicating trend in activity
seaborn lineplot time-series indicating trend in activity

Time:06-10

On a seaborn lineplot, I would like to indicate trend in a time-series data, preferably using different colours.

For example, taking this fake data:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame(np.random.randint(100, size=50), columns=['max'])
df['day'] = pd.date_range('2016-1-1', periods=50, freq='SMS')#freq='W')
df['date'] = df['day'].dt.strftime('%Y-%m')

On a lineplot this produces the following figure:

sns.lineplot(data=df, x = df['date'], y='max', )
plt.xticks(rotation=45)

enter image description here

So I would like to indicate the trend in time series between 2017-01 and 2017-08 such that the plot's background in this area is in green, with begin and end marked (similar to the figure below, but inserting green background in the area indicated). enter image description here

CodePudding user response:

You can use enter image description here

alternative with a different zorder:

ax.axvspan('2017-01', '2017-08', color='g', alpha=0.5, zorder=0)

output:

enter image description here

  • Related