Home > Net >  format x-axis (dates) in sns.lmplot()
format x-axis (dates) in sns.lmplot()

Time:10-26

I have daily data that I need to plot with sns.lmplot().

The data has the following structure:

df = pd.DataFrame(columns=['date', 'origin', 'group', 'value'],
                  data = [['2001-01-01', "Peter", "A", 1.0],
                          ['2011-01-01', "Peter", "A", 1.1],
                          ['2011-01-02', "Peter", "B", 1.2],
                          ['2012-01-03', "Peter", "A", 1.3],
                          ['2012-01-01', "Peter", "B", 1.4],
                          ['2013-01-02', "Peter", "A", 1.5],
                          ['2013-01-03', "Peter", "B", 1.6],
                          ['2021-01-01', "Peter", "A", 1.7]])

I now want to plot the data with sns.lmplot() for monthly averages (my original data is more fine-grained than the toy data) and using the hue for group-column. For this, I aggregate by month:

df['date'] = pd.to_datetime(df['date']).dt.strftime('%Y%M').astype(int)
df = df.groupby(['date', 'origin', 'group']).agg(['mean'])
df.columns = ["_".join(pair) for pair in df.columns]  # reset col multi-index
df = df.reset_index()  # reset index

Then I plot the data:

sns.lmplot(data=df, x="date", y="value", hue="group",
           ci=None, truncate=False, scatter_kws={"s": 1}, lowess=True, height=6, aspect=1.25)
plt.title(f"Title.")
plt.ylabel("Value")
plt.show()

This works fine but the dates are messy. I would like them to be displayed as dates rather than ints.

I have found enter image description here

  • Related