0

Below, I plot the following Figure in Python:Figure

As you can see the plot on the right is much more "smooth" than the one on the left. That's because the scaling of x-axis on both plot is different. More observations on the left than on the right (about three times more). Hence how can I "squeeze" horizontally the right plot such that I get somewhat an approximative look to the one of the left? Below is my code (I use Pandas):

fig, axes = plt.subplots(1, 2, sharey=True, figsize=(30, 15))
        # plot the same data on both axes
        #gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])
        ax1 = df1.plot(ax=axes[0], grid='off', legend=False,
                                       xticks=[-250, -200, -150, -100, -50,
                                               0, 25], lw=2, colormap='jet',
                                       fontsize=20)
        ax2 = df2.plot(ax=axes[1], grid='off', legend=False,
                                       xticks=[-5, 0, 20, 40, 60, 80], lw=2,
                                       colormap='jet', fontsize=20)
        # zoom-in / limit the view to different portions of the data
        # hide the spines between ax and ax2
        ax1.set_ylabel('Treatment-Control Ratio', fontsize=20)
        ax1.axhline(y=1, color='r', linewidth=1.5)
        ax2.axhline(y=1, color='r', linewidth=1.5)
        ax1.axvline(x=0, color='r', linewidth=1.5, linestyle='--')
        ax2.axvline(x=0, color='r', linewidth=1.5, linestyle='--')
        ax1.set_xlabel('Event Time - 1 Minute', fontsize=20)
        ax2.set_xlabel('Event Time - 1 Minute', fontsize=20)
        ax1.spines['right'].set_visible(False)
        ax2.spines['left'].set_visible(False)
        ax1.yaxis.tick_left()
        ax2.yaxis.set_major_locator(plt.NullLocator())
        ax1.tick_params(labeltop='off')  # don't put tick labels at the top
        plt.subplots_adjust(wspace=0.11)
        plt.tight_layout()
Plug4
  • 3,838
  • 9
  • 51
  • 79
  • 1
    http://stackoverflow.com/questions/10388462/matplotlib-different-size-subplots – gboffi Apr 07 '15 at 21:45
  • I see. But how can I use `GridSpec` when I use the `plot` built-in function in Pandas? – Plug4 Apr 07 '15 at 22:12
  • 1
    `pandas.plot` has a keyword parameter `ax`. Get the axes from the `GridSpec` layout as the example @gboffi links to does, and pass them to `df.plot`. – cphlewis Apr 07 '15 at 22:28

1 Answers1

1

With the help of @cphlewis and @gboffi I fixed the issue with the code below:

fig, axes = plt.subplots(figsize=(30, 15))
        # plot the same data on both axes
        gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1.2]) 
        ax1 = plt.subplot(gs[0])
        ax2 = plt.subplot(gs[1], sharey=ax1)
        df_wpc.loc[-260:25].plot(ax=ax1, grid='off', legend=False,
                                       xticks=[-250, -200, -150, -100, -50,
                                               0, 25], lw=2, colormap='jet',
                                       fontsize=20)
        df_pc_et.loc[-5:91].plot(ax=ax2, grid='off', legend=False,
                                       xticks=[-5, 0, 20, 40, 60, 80], lw=2,
                                       colormap='jet', fontsize=20)
        ax1.set_ylabel('Treatment-Control Ratio', fontsize=20)
        ax1.axhline(y=1, color='r', linewidth=1.8)
        ax2.axhline(y=1, color='r', linewidth=1.8)
        ax1.axvline(x=0, color='r', linewidth=1.8, linestyle='--')
        ax2.axvline(x=0, color='r', linewidth=1.8, linestyle='--')
        ax1.set_xlabel('Event Time - 1 Minute', fontsize=20)
        ax2.set_xlabel('Event Time - 1 Minute', fontsize=20)
        ax1.spines['right'].set_visible(False)
        ax2.spines['left'].set_visible(False)
        ax1.yaxis.tick_left()
        ax2.yaxis.set_major_locator(plt.NullLocator())
        ax1.tick_params(labeltop='off')  # don't put tick labels at the top
        plt.subplots_adjust(wspace=0.7)
        plt.tight_layout()
Plug4
  • 3,838
  • 9
  • 51
  • 79
  • You could even calculate the width ratios from the data slices so that they plot in the same x-metric. – cphlewis Apr 07 '15 at 23:21