如何解决在子图之间共享辅助 y 轴 - Python
我想创建多个子图(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',length=6)
ax.tick_params(axis='both',length=6)
ax.tick_params(axis='both',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,r'$Number \ of \ points$',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)
所有 y 次轴和所有子图都没有刻度。但是,我想查看子图编号 3 和 6 的刻度
解决方法
找到解决办法:
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',length=6)
ax.tick_params(axis='both',length=6)
ax.tick_params(axis='both',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,r'$Number \ of \ points$',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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。