猿问

在matplotlib的子图中嵌入小图

如果要在较大的图中插入一个较小的图,则可以使用Axes,例如此处。


问题是我不知道如何在子图中执行相同的操作。


我有几个子图,我想在每个子图中绘制一个小图。示例代码将如下所示:


import numpy as np

import matplotlib.pyplot as plt


fig = plt.figure()


for i in range(4):

    ax = fig.add_subplot(2,2,i)

    ax.plot(np.arange(11),np.arange(11),'b')


    #b = ax.axes([0.7,0.7,0.2,0.2]) 

    #it gives an error, AxesSubplot is not callable


    #b = plt.axes([0.7,0.7,0.2,0.2])

    #plt.plot(np.arange(3),np.arange(3)+11,'g')

    #it plots the small plot in the selected position of the whole figure, not inside the subplot

有任何想法吗?


提前致谢!


DIEA
浏览 1905回答 3
3回答

宝慕林4294392

我写了一个非常类似于plt.axes的函数。您可以使用它来绘制子子图。有一个例子...import matplotlib.pyplot as pltimport numpy as npdef add_subplot_axes(ax,rect,axisbg='w'):&nbsp; &nbsp; fig = plt.gcf()&nbsp; &nbsp; box = ax.get_position()&nbsp; &nbsp; width = box.width&nbsp; &nbsp; height = box.height&nbsp; &nbsp; inax_position&nbsp; = ax.transAxes.transform(rect[0:2])&nbsp; &nbsp; transFigure = fig.transFigure.inverted()&nbsp; &nbsp; infig_position = transFigure.transform(inax_position)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; x = infig_position[0]&nbsp; &nbsp; y = infig_position[1]&nbsp; &nbsp; width *= rect[2]&nbsp; &nbsp; height *= rect[3]&nbsp; # <= Typo was here&nbsp; &nbsp; subax = fig.add_axes([x,y,width,height],axisbg=axisbg)&nbsp; &nbsp; x_labelsize = subax.get_xticklabels()[0].get_size()&nbsp; &nbsp; y_labelsize = subax.get_yticklabels()[0].get_size()&nbsp; &nbsp; x_labelsize *= rect[2]**0.5&nbsp; &nbsp; y_labelsize *= rect[3]**0.5&nbsp; &nbsp; subax.xaxis.set_tick_params(labelsize=x_labelsize)&nbsp; &nbsp; subax.yaxis.set_tick_params(labelsize=y_labelsize)&nbsp; &nbsp; return subaxdef example1():&nbsp; &nbsp; fig = plt.figure(figsize=(10,10))&nbsp; &nbsp; ax = fig.add_subplot(111)&nbsp; &nbsp; rect = [0.2,0.2,0.7,0.7]&nbsp; &nbsp; ax1 = add_subplot_axes(ax,rect)&nbsp; &nbsp; ax2 = add_subplot_axes(ax1,rect)&nbsp; &nbsp; ax3 = add_subplot_axes(ax2,rect)&nbsp; &nbsp; plt.show()def example2():&nbsp; &nbsp; fig = plt.figure(figsize=(10,10))&nbsp; &nbsp; axes = []&nbsp; &nbsp; subpos = [0.2,0.6,0.3,0.3]&nbsp; &nbsp; x = np.linspace(-np.pi,np.pi)&nbsp; &nbsp; for i in range(4):&nbsp; &nbsp; &nbsp; &nbsp; axes.append(fig.add_subplot(2,2,i))&nbsp; &nbsp; for axis in axes:&nbsp; &nbsp; &nbsp; &nbsp; axis.set_xlim(-np.pi,np.pi)&nbsp; &nbsp; &nbsp; &nbsp; axis.set_ylim(-1,3)&nbsp; &nbsp; &nbsp; &nbsp; axis.plot(x,np.sin(x))&nbsp; &nbsp; &nbsp; &nbsp; subax1 = add_subplot_axes(axis,subpos)&nbsp; &nbsp; &nbsp; &nbsp; subax2 = add_subplot_axes(subax1,subpos)&nbsp; &nbsp; &nbsp; &nbsp; subax1.plot(x,np.sin(x))&nbsp; &nbsp; &nbsp; &nbsp; subax2.plot(x,np.sin(x))if __name__ == '__main__':&nbsp; &nbsp; example2()&nbsp; &nbsp; plt.show()

牛魔王的故事

您现在可以使用matplotlibs inset_axes方法执行此操作(请参阅docs):from mpl_toolkits.axes_grid.inset_locator import inset_axesinset_axes = inset_axes(parent_axes,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width="30%", # width = 30% of parent_bbox&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height=1., # height : 1 inch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loc=3)更新:正如Kuti所指出的,对于matplotlib 2.1版或更高版本,您应该将import语句更改为:from mpl_toolkits.axes_grid1.inset_locator import inset_axes现在,还有一个完整的示例显示了所有可用的选项。
随时随地看视频慕课网APP
我要回答