如果你想在较大的一个中插入一个小图,你可以使用Axes,如here .
问题是我不知道如何在子图中做同样的事情 .
我有几个子图,我想在每个子图中绘制一个小图 . 示例代码将是这样的:
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
有任何想法吗?
提前致谢!
4 回答
我写了一个与plt.axes非常相似的函数 . 您可以使用它来绘制您的子子图 . 有一个例子......
来源:https://matplotlib.org/examples/pylab_examples/axes_demo.html
您现在可以使用matplotlibs
inset_axes
方法执行此操作(请参阅docs):Update: 正如Kuti所指出的,对于matplotlib 2.1或更高版本,您应该将import语句更改为:
现在还有一个full example显示所有可用的选项 .
从matplotlib 3.0开始,您可以使用matplotlib.axes.Axes.inset_axes:
在@jrieke's answer中提到的
mpl_toolkits.axes_grid.inset_locator.inset_axes
的区别在于它更容易使用(没有额外的进口等),但缺点是灵活性稍差(没有填充或角落位置的参数) .