1

I want to plot 2 subplots with their x-axis being date (which is the same for both x axis). However, when i do the plot, only one of the subplots got the xticks format i wanted. How do i make the xtick format work for both subplots?

trend1 = pd.read_sql(query1, conn)
trend1['date'] = pd.to_datetime(trend1['date']).dt.date

trend2 = pd.read_sql(query2, conn)
trend2['date'] = pd.to_datetime(trend2['date']).dt.date

fig, ax = plt.subplots(ncols=2, figsize=(15,4))  


data1 = trend1.set_index('date').sort_index().reset_index()
data2 = trend2.set_index('date').sort_index().reset_index()

sns.lineplot(data = data1, 
              x='date', y='count_1', hue='country', ax=ax[0])

sns.lineplot(data = data2, 
              x='date', y='count_2', hue='country', ax=ax[1])


plt.xticks(
    rotation=45, 
    horizontalalignment='right',
    fontweight='light',
    fontsize='small'  
)

enter image description here

jxn
  • 7,685
  • 28
  • 90
  • 172
  • Maybe you can change the last line of the code shown to `ax[1].set_xticklabels(...)` instead of to `ax[0]....`? That way you would change the ticks of the second subplot. – JohanC May 16 '20 at 12:24
  • Thanks! i fixed that and format looks better, but date seems jumbled up still – jxn May 16 '20 at 13:21

1 Answers1

0

From an answer here

axes[0].tick_params(axis='x', labelrotation=45)
axes[1].tick_params(axis='x', labelrotation=45)
Relative0
  • 1,567
  • 4
  • 17
  • 23