I have a data frame df
that looks like this:
DateTime code value
0 2021-03-25 16:35:49 1 29.0
1 2021-03-25 16:35:49 2 15.0
2 2021-03-25 16:36:09 1 15.0
3 2021-03-25 16:36:09 2 29.0
4 2021-03-25 16:36:51 1 29.0
...
I'd like to make a lineplot of the values with confidence intervals, where the signals are grouped by week. This should be really easy to do with seaborn, but seaborn doesn't seem to allow Period
datatypes as values for the x-axis. Running this gives an Invalid object type
error:
df['week'] = df.DateTime.dt.to_period('W')
sns.lineplot(data=df, x='week', y='value', hue='code', ci='sd')
I can change the datatype to a timestamp and plot:
df['week'] = df.DateTime.dt.to_period('W')
df.week = df.week.dt.to_timestamp()
sns.lineplot(data=df, x='week', y='value', hue='code', ci='sd')
This gives me close to what I want, but I'd rather have the x-axis label look like it would if I had plotted it directly with matplotlib. But I can't figure out an easy way to plot the confidence intervals filled in as they are in the seaborn plot.
df['week'] = df.DateTime.dt.to_period('W')
df_week = df.groupby(['week','code'])['value'].agg(['mean']).reset_index()
ax = df_week .query("code==1").plot(x='week', y='mean', label='1')
df_week.query("code==2").plot(ax=ax, x='week', y='mean', label='2')
How can I easily get the plot from the first figure, but with the nice-looking x-axis labels from the second?