1

I have a time series data and I plot two charts side by side. I need to rotate x labels as there are too may years to show. When I add plt.xticks(rotation=45) it changes rotation only for the second plot but not the first. Any suggestions?

This is my code:

values = range(0,120)
dates = pd.date_range(start='2000', end='2010', freq='M')
df = pd.DataFrame({'Date': dates,
                     'value':values})

def box_plot(df, value):
    # Prepare data
    data_date = df.reset_index()
    data_date['year'] = [d.year for d in data_date.Date]
    data_date['month'] = [d.strftime('%b') for d in data_date.Date]
    years = data_date['year'].unique()

    # Draw Plot
    fig, axes = plt.subplots(1, 2, figsize=(20,7), dpi= 80)
    sns.boxplot(x='year', y=value, data=data_date, ax=axes[0])    
    sns.boxplot(x='month', y=value, data=data_date)

    # Set Title
    axes[0].set_title('Year-wise Box Plot '+value+'\n(The Trend)', fontsize=18); 
    axes[1].set_title('Month-wise Box Plot '+value+'\n(The Seasonality)', fontsize=18)
    plt.show()
William Miller
  • 9,839
  • 3
  • 25
  • 46
aviss
  • 2,179
  • 7
  • 29
  • 52

1 Answers1

3

You should be able to use the matplotlib.axes.Axes.tick_params() method instead of plt.xticks. The latter only applies to the 'current', in this case most recently generated, axes instance while the former can be used on any axes instance. For example:

axes[0].tick_params(axis='x', labelrotation=45)
axes[1].tick_params(axis='x', labelrotation=45)
William Miller
  • 9,839
  • 3
  • 25
  • 46