Search code examples
numpymatplotlibplotmultiple-axes

label_outer() for twinx in many subplots


I am trying to plot many subplots with each subplot showing 2 y-data sets using secondary axis twinx. However, i only want to show yticks labels at outer locations. This is easily done with label_outer() for the original data, but i can't figure out how to do it for the secondary axes.

import numpy as np
import matplotlib.pyplot as plt

x=np.arange(0,5,step=1)
y1=np.sin(x)
y2=10*np.cos(x)

fig, axs = plt.subplots(4, 3, figsize=(7.2,9.7),dpi=300,sharex='col', sharey='row',
                        gridspec_kw={'hspace': 0.2, 'wspace': 0.2})

for n,k in enumerate(np.arange(1,13,step=1)):
    ax2 = axs.flat[n].twinx()
    axs.flat[n].plot(x,y1,c='black')
    ax2.plot(x,y2,c='deepskyblue')

axs.flat[n].set_xlabel('Frequency (Hz)',fontsize=9)

for ax in axs.flat:
    ax.label_outer()

plt.tight_layout()
plt.show()

b

As you can see the right ticklabels are all over the figure, i want them to only be at the edges, but not sure how to accomplish this. Thank you


Solution

  • So the answer is simply add ax2.set_yticklabels([]) to remove secondary yticks labels.