0

I want to create a number of subplots (2 rows, 3 cols) each of which has a secondary y-axis. I want the secondary y-axis to be shared amongst all the subplots. As a result, I want to remove the ticks for the subplots in the left and middle and only leave the ticks for the top and bottom right-hand side plots. This is my code:

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)

This is what I am getting. enter image description here

No ticks at all for all y secondary axes and all subplots. However,I would like to see the ticks for subplots number 3 and 6

asked Jul 6, 2021 at 15:02

1 Answer 1

2

Found the solution:

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)
answered Jul 6, 2021 at 16:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.