【问题标题】:How to add an inset_axes to a subplot with matplotlib如何使用 matplotlib 将 inset_axes 添加到子图
【发布时间】:2018-03-03 22:04:12
【问题描述】:

我正在尝试在 matplotlib 中绘制多个子图,每个子图都应该有一个插入轴。我可以让代码示例适用于带有使用mpl_toolkits.axes_grid.inset_locator.inset_axes() 添加的插入轴的单轴,并且我可以在没有插入轴的情况下很好地绘制子图,但是当尝试对循环中的子图执行相同操作时,我得到@987654324 @ 在第二个子情节。这似乎有点奇怪,当number_of_plots 为 ==1 但不是 >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
                            )

【问题讨论】:

    标签: python matplotlib subplot


    【解决方案1】:

    对于未来的读者:This post 展示了在 matplotlib 中创建插图的方法。


    这里问题的具体答案:这不是一个错误。你正在重新定义inset_axes

    inset_axes = inset_axes(...) 行之前,inset_axes 是来自mpl_toolkits.axes_grid.inset_locator 的函数。之后,inset_axes 是该函数的返回值,即AxesHostAxes

    一般建议当然是:切勿调用与您在代码中导入或使用的函数同名的变量。

    具体解决方案:

    ax_ins = inset_axes(ax, width="30%",  height="30%")
    

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 2016-04-28
      • 2016-11-29
      • 2018-08-15
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      相关资源
      最近更新 更多