子图嵌套的问题大家通常都会用fig.add_axes在一个fig对象上进行操作。但如果我们有多个子图,但只想在一个子图上再嵌套一个子图呢?我查了很多地方都没有找到解决方法。于是求助于matplotlib gallery,果然有参考的例子,只需要调用inset_axes的操作就可以在ax对象上操作了。
下面给大家做一个参考:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
myfont={'family':'Times New Roman',
'style':'normal',
'weight':'normal',
'color':'black',
'size':15}
myfont1={'family':'Times New Roman',
'style':'normal',
'weight':'normal',
'color':'red',
'size':13}
fig=plt.figure()
ax1=fig.add_subplot(121)
ax1.text(0.5,0.5,"ax1",myfont)
axins1=inset_axes(ax1,width="30%", height="40%", loc=1)
axins1.text(0.25,0.5,"axins1",myfont1)
#loc可用来改变位置,同时也可以用bbox_to_anchor调节
ax2=fig.add_subplot(222)
ax2.text(0.5,0.5,"ax2",myfont)
axins2=inset_axes(ax2,width="40%", height="30%", loc=2)
axins2.text(0.3,0.5,"axins2",myfont1)
ax3=fig.add_subplot(224)
ax3.text(0.5,0.5,"ax3",myfont)
axins3=inset_axes(ax3,width="40%", height="30%",loc=3)
axins3.text(0.3,0.5,"axins3",myfont1)
for axi in [ax1,ax2,ax3,axins1, axins2, axins3]:
axi.tick_params(labelleft=False, labelbottom=False)#关闭嵌套图坐标轴
fig.show()

1808




被折叠的 条评论
为什么被折叠?



