fig = plt.figure(figsize=(fig_width, fig_length),frameon=True) ax = fig.subfigures(3,1,width_ratios=[0.2], height_ratios= [0.8,0.8,0.8]) ####################################################################### axs = ax[0].subplots(1, 2, gridspec_kw={'width_ratios': [15, 1]}) axs[1].axis('off') #plt.subplots_adjust(left=0.1,right=1) extent=[0,30,-8,8] im1=axs[0].imshow(eyave,extent=extent,cmap='RdBu_r',vmin=-6e12,vmax=6e12) im2=axs[0].imshow(ele,extent=extent,cmap=my_cmapEy,vmin=0,vmax=10) axs[0].set_yticks([-8,-4,0,4,8]) #色条位置 imcb1=ax[0].add_axes([0.32, 0.12,0.6,0.75]) imcb1.axis('off') #imcb2=fig.add_axes([0.47, 0.15,0.5,0.7]) #imcb2.axis('off') #cb2=fig.colorbar(im2,ax=imcb2,ticks=[0,5,10,15],shrink=1,aspect=16) cb1=fig.colorbar(im1,ax=imcb1,ticks=[-6e12,-3e12,0,3e12,6e12],shrink=0.88,aspect=14) cb1.set_ticklabels(['-6', '-3', '0','3','6']) #axs[0].annotate('×$10^{12}$', xy=(3, 0), xytext=(30,8.5)) axs[0].annotate('×$10^{12}$', xy=(3, 0), xytext=(30,8.5)) axs[0].annotate('with $B_x$', xy=(3, 0), xytext=(24,6)) axs[0].annotate('w/o $B_x$', xy=(3, 0), xytext=(24,-6.5)) axs[0].annotate('y (μm)', xy=(3, 0), xytext=(-4,-2),rotation=90) #axs[0].annotate('x (μm)', xy=(3, 0), xytext=(13,-11)) fig.text(0,0,"x (μm)", fontsize=80) plt.annotate('x (μm)', xy=(3, 0), xytext=(0.5,0.5))#(13,-11.3))所加标签没有显示
时间: 2023-08-30 12:04:10 浏览: 85
matplotilb中fig,ax = plt.subplots()的使用
在您的代码中,您使用了`fig.text()`和`plt.annotate()`两种方法添加了x轴标签。但是,它们都没有显示在图中。
可能的原因是,您的x轴标签被其他元素遮挡了。您可以尝试使用`zorder`参数来设置元素的绘制顺序,以确保标签在最上层绘制。例如,将`fig.text()`和`plt.annotate()`的`zorder`参数设置为更高的值,如下所示:
```
fig.text(0.5, 0, 'x (μm)', ha='center', fontsize=14, zorder=10)
plt.annotate('x (μm)', xy=(3, 0), xytext=(0.5, 0.5), ha='center', va='center', fontsize=14, zorder=10)
```
此外,您还需要将`frameon`参数设置为`False`,以避免图形边框遮挡标签。完整的代码如下:
```
fig = plt.figure(figsize=(fig_width, fig_length), frameon=False)
ax = fig.subfigures(3,1,width_ratios=[0.2], height_ratios= [0.8,0.8,0.8])
axs = ax[0].subplots(1, 2, gridspec_kw={'width_ratios': [15, 1]})
axs[1].axis('off')
extent=[0,30,-8,8]
im1=axs[0].imshow(eyave,extent=extent,cmap='RdBu_r',vmin=-6e12,vmax=6e12)
im2=axs[0].imshow(ele,extent=extent,cmap=my_cmapEy,vmin=0,vmax=10)
axs[0].set_yticks([-8,-4,0,4,8])
imcb1=ax[0].add_axes([0.32, 0.12,0.6,0.75])
imcb1.axis('off')
cb1=fig.colorbar(im1,ax=imcb1,ticks=[-6e12,-3e12,0,3e12,6e12],shrink=0.88,aspect=14)
cb1.set_ticklabels(['-6', '-3', '0','3','6'])
axs[0].annotate('×$10^{12}$', xy=(3, 0), xytext=(30,8.5))
axs[0].annotate('with $B_x$', xy=(3, 0), xytext=(24,6))
axs[0].annotate('w/o $B_x$', xy=(3, 0), xytext=(24,-6.5))
axs[0].annotate('y (μm)', xy=(3, 0), xytext=(-4,-2),rotation=90)
fig.text(0.5, 0, 'x (μm)', ha='center', fontsize=14, zorder=10)
plt.annotate('x (μm)', xy=(3, 0), xytext=(0.5, 0.5), ha='center', va='center', fontsize=14, zorder=10)
plt.show()
```
请注意,`fig.text()`中的`ha`参数是`center`,以使标签在x轴中心对齐,而`plt.annotate()`中的`ha`和`va`参数均为`center`,以使标签在指定的坐标处居中对齐。如果您仍然无法看到x轴标签,请尝试调整`zorder`参数的值。
阅读全文