[英]How to add an inset_axes to a subplot with matplotlib
I am trying to plot a number of subplots in matplotlib, each subplot should have an inset axes.我正在尝试 plot matplotlib 中的多个子图,每个子图都应该有一个插入轴。 I can get the code examples to work for a single axis with a inset axes added using mpl_toolkits.axes_grid.inset_locator.inset_axes()
, and I can plot subplots fine without the inset axes, but when trying to do the same for subplots in a loop, I get TypeError: 'AxesHostAxes' object is not callable
on the second subplot .我可以让代码示例适用于带有使用mpl_toolkits.axes_grid.inset_locator.inset_axes()
添加的插入轴的单个轴,并且我可以在没有插入轴的情况下很好地 plot 子图,但是当尝试对 a 中的子图执行相同操作时循环,我得到TypeError: 'AxesHostAxes' object is not callable
on the second subplot 。 This seems a bit strange that it should work when number_of_plots
is ==1, but not >1.这似乎有点奇怪,当number_of_plots
为 ==1 但不是 >1 时它应该起作用。 How should I be doing it, or is it a bug?我应该怎么做,或者它是一个错误? ( matplotlib.__version__
is '1.5.1') ( matplotlib.__version__
是 '1.5.1')
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid.inset_locator import inset_axes
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
n_row, n_col = 4, 4
fig = plt.figure(1,(10,10))
#number_of_plots = 1 Works!
number_of_plots = n_row * n_col # Does not work!
for idx in range(number_of_plots):
ax = fig.add_subplot(n_row, n_col, idx + 1)
ax.plot(x, y)
inset_axes = inset_axes(ax,
width="30%", # width = 30% of parent_bbox
height="30%", # height : 1 inch
)
For future readers: This post shows methods to create insets in matplotlib. 对于未来的读者: 这篇文章展示了在matplotlib中创建insets的方法。
inset_axes
.
您正在重新定义inset_axes
。
Before the line inset_axes = inset_axes(...)
, inset_axes
is a function from mpl_toolkits.axes_grid.inset_locator
. 在行inset_axes = inset_axes(...)
, inset_axes
是来自mpl_toolkits.axes_grid.inset_locator
的函数。 After that, inset_axes
is the return of that function, which is an AxesHostAxes
. 之后, inset_axes
是该函数的返回,它是一个AxesHostAxes
。
The general advice is of course: Never call a variable by the same name as a function you import or use in your code. 一般建议当然是: 永远不要使用与导入或在代码中使用的函数相同的名称来调用变量。
The concrete solution: 具体解决方案:
ax_ins = inset_axes(ax, width="30%", height="30%")
Good looking out big dawg, that helped me with a similar issue.很好看的大笨蛋,这帮助我解决了类似的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.