在子图之间共享辅助 y 轴 - Python

时间:2021-07-06 15:02:20

标签: python matplotlib axis2 subplot figure

我想创建多个子图(2 行,3 列),每个子图都有一个辅助 y 轴。我希望在所有子图之间共享辅助 y 轴。因此,我想删除左侧和中间子图的刻度,只留下顶部和底部右侧图的刻度。这是我的代码:

def plot_example(n_rows, n_cols):

    def two_scales(ax1, c1, c2):
        ax2 = ax1.twinx()
        return ax1, ax2

    fig, axs = plt.subplots(nrows=n_rows, ncols=n_cols, sharex=True, sharey=True, figsize=(25,15))
    fig.subplots_adjust(hspace = .15, wspace=.15)
    data = np.arange(0,n_rows*n_cols) 

    uu  = 0
    for ax, kk in zip(axs.ravel(),data):
        ### apply function ###
        ax, ax2 = two_scales(ax, 'darkred', 'darkslateblue')

        ### Plot the two figures ###
        ax.scatter(np.random.rand(100),np.random.rand(100)*1e5,c ='darkslateblue')
        ax2.scatter(np.random.rand(100),np.random.rand(100)*1e2,c ='darkred')
 
        #### Set tick parameters ####
        ax2.tick_params(axis='both', which='major', labelsize=25, width=1 ,length=6)
        ax2.tick_params(axis='both', which='minor', labelsize=25, width=1 ,length=6)
     
        ax.tick_params(axis='both', which='major', labelsize=25, width=1 ,length=6)
        ax.tick_params(axis='both', which='minor', labelsize=25, width=1 ,length=6 )
        
        #### Set axis scale ####
        ax2.set_yscale('log')
        ax.set_yscale('log')

        ### Change color of each axis ###
        def color_y_axis(ax, color):
            """Color your axes."""
            for t in ax.get_yticklabels():
                t.set_color(color)

        color_y_axis(ax, 'darkslateblue')
        color_y_axis(ax2, 'darkred')
        
        ### This is how I try to remove the ticks from the secondary axis for subplots 0,1 and 3,4 ###
        if (uu!=2) or (uu!=5):
            ax2.set_yticks([])
        elif (uu==2) or (uu==5):
            ax2.set_yticks([1e1, 1e3, 1e5])
        
        uu= uu+1

    fig.text(0.07, 0.5, r'$\langle T \rangle \  \mathcal{(K)}$', va='center', rotation='vertical',color='darkslateblue',fontsize=font_size+15)
    fig.text(0.92, 0.5, r'$Number \ of \ points$', va='center', rotation='vertical',color='darkred',fontsize=font_size+15)
   
    fig.text(0.5, 0.05, '$\mathcal{PVI}$', ha='center',fontsize=font_size+15)

### run defined function ###
n_rows = 2
n_cols =3
plot_example(n_rows, n_cols)

这就是我得到的。 enter image description here

所有 y 次轴和所有子图都没有刻度。但是,我想查看子图编号 3 和 6 的刻度

1 个答案:

答案 0 :(得分:1)

找到解决办法:

def plot_example(n_rows, n_cols):

    def two_scales(ax1, c1, c2):
        ax2 = ax1.twinx()
        return ax1, ax2

    fig, axs = plt.subplots(nrows=n_rows, ncols=n_cols, sharex=True, sharey=True, figsize=(25,15))
    fig.subplots_adjust(hspace = .15, wspace=.15)
    data = np.arange(0,n_rows*n_cols) 
   
    uu  = 0
    for ax, kk in zip(axs.ravel(),data):
        ### apply function ###
        ax, ax2 = two_scales(ax, 'darkred', 'darkslateblue')

        ### Plot the two figures ###
        ax.scatter(np.random.rand(100),np.random.rand(100)*1e5,c ='darkslateblue')
        ax2.scatter(np.random.rand(100),np.random.rand(100)*1e2,c ='darkred')
 
        #### Set tick parameters ####
        ax2.tick_params(axis='both', which='major', labelsize=25, width=1 ,length=6)
        ax2.tick_params(axis='both', which='minor', labelsize=25, width=1 ,length=6)
     
        ax.tick_params(axis='both', which='major', labelsize=25, width=1 ,length=6)
        ax.tick_params(axis='both', which='minor', labelsize=25, width=1 ,length=6 )
        
        #### Set axis scale ####
        ax2.set_yscale('log')
        ax.set_yscale('log')

        ### Change color of each axis ###
        def color_y_axis(ax, color):
            """Color your axes."""
            for t in ax.get_yticklabels():
                t.set_color(color)

        color_y_axis(ax, 'darkslateblue')
        color_y_axis(ax2, 'darkred')
        
        ### This is how I try to remove the ticks from the secondary axis for subplots 0,1 and 3,4 ###
        if (uu!=2) and (uu!=5):
            ax2.set_yticks([])

        
        uu= uu+1

    fig.text(0.07, 0.5, r'$\langle T \rangle \  \mathcal{(K)}$', va='center', rotation='vertical',color='darkslateblue',fontsize=font_size+15)
    fig.text(0.92, 0.5, r'$Number \ of \ points$', va='center', rotation='vertical',color='darkred',fontsize=font_size+15)
   
    fig.text(0.5, 0.05, '$\mathcal{PVI}$', ha='center',fontsize=font_size+15)

    ### run defined function ###
    n_rows = 2
    n_cols =3
    plot_example(n_rows, n_cols)