Home > OS >  Seaborn - Change the X-Axis Range (Date field)
Seaborn - Change the X-Axis Range (Date field)

Time:07-13

how can I change the x-axis so that I begin on January 1 2022? I don't want to set the other side of the bound. The aim here is to create a YTD chart. Thanks! (Data type for the x-axis field 'Date_reported' is a Dtype datetime64[ns]) (ps: does anyone know why my figsize statement isn't working? I'm aiming for the 15 by 8 siz but it doesn't seem to work. enter image description here

sns.relplot(kind='line', data=df_Final, x='Date_reported', y='New_cases_Mov_avg',
                        hue='Continent', linewidth=1, ci=None)
sns.set_style("white")
sns.set_style('ticks')
plt.xlabel("Date Reported")
plt.ylabel("New Cases (Moving Average)")
plt.figure(figsize=(15,8))

CodePudding user response:

You could define your figure and ax beforehand, set the figsize and then plot. Doing so, you have to go with lineplot instead of relplot. ax.set_xlim will define the left boundary, fig.autofmt_xdate rotates the x labels.

fig, ax = plt.subplots(figsize=(15,8))
sns.lineplot(data=df_Final, x='Date_reported', y='New_cases_Mov_avg',
                        hue='Continent', linewidth=1, ci=None, ax=ax)
sns.set_style("white")
sns.set_style('ticks')
ax.set_xlabel("Date Reported")
ax.set_ylabel("New Cases (Moving Average)")
ax.set_xlim(datetime.date(2022, 1, 1))
fig.autofmt_xdate()
  • Related