首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >单击时放大子图(带有inset_axes图)

单击时放大子图(带有inset_axes图)
EN

Stack Overflow用户
提问于 2020-12-02 21:08:00
回答 1查看 46关注 0票数 0

所以我遵循了这个帖子中的方法:

How can I plot the same figure standalone and in a subplot in Matplotlib?

正如他的例子所示,它实际上工作得很好。然而,有一个问题是我的图中有inset_axes图。因此,当它缩放时,所有的插图都保留下来,实际上与放大的图重叠。

然而,我不确定如何删除它们,甚至可能还放大与正在单击的子图一起的插图。

因此,在线程中,我使用了这个类来实现缩放:

代码语言:javascript
运行
复制
class ZoomingSubplots(object):
    def __init__(self, *args, **kwargs):
        """All parameters passed on to 'subplots`."""
        self.fig, self.axes = plt.subplots(*args, **kwargs)
        self._zoomed = False
        self.fig.canvas.mpl_connect('button_press_event', self.on_click)
        self.fig.subplots_adjust(hspace=0.3)

    def zoom(self, selected_ax):
        for ax in self.axes.flat:
            ax.set_visible(False)
        self._original_size = selected_ax.get_position()
        selected_ax.set_position([0.125, 0.1, 0.775, 0.8])
        selected_ax.set_visible(True)
        self._zoomed = True

    def unzoom(self, selected_ax):
        selected_ax.set_position(self._original_size)
        for ax in self.axes.flat:
            ax.set_visible(True)
        self._zoomed = False

    def on_click(self, event):
        if event.inaxes is None:
            return
        if self._zoomed:
            self.unzoom(event.inaxes)
        else:
            self.zoom(event.inaxes)
        self.fig.canvas.draw()

然后,当我绘制时,我使用这个:

代码语言:javascript
运行
复制
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

__name__ = '__main__':
    subplots = ZoomingSubplots(2, 2)

    for ax in subplots.axes.flat:
        ax.plot(x, y)
        axins = inset_axes(ax, width=1.3, height=0.9, loc=2)
        axins.plot(x2, y2)

    plt.show()

但如上所述,所有子图的插图将保持在其位置,并与放大的图重叠。我如何才能改变这一点,这样他们就不会干预,甚至连次要情节的inset_plot也会保留并放大?

EN

Stack Overflow用户

回答已采纳

发布于 2020-12-02 23:22:43

我找不到一种方法来改变inset_axes的位置,可能是因为它们的位置和大小都锁定在它们的父轴上。

同时,这里有一个新的类,它自动创建inset_axes,并在单击其中一个基轴时隐藏/显示相关的inset_axes。

代码语言:javascript
运行
复制
class ZoomingSubplotsWithInset(ZoomingSubplots):
    def __init__(self, *args, inset_width=1.3, inset_height=0.9, inset_loc=2, **kwargs):
        super(ZoomingSubplotsWithInset, self).__init__(*args, **kwargs)
        self.inset_axes = []
        for ax in self.axes.flat:
            axins = inset_axes(ax, width=inset_width, height=inset_height, loc=inset_loc)
            self.inset_axes.append(axins)
        self.inset_axes = np.array(self.inset_axes)

    def on_click(self, event):
        if event.inaxes in self.axes.flat:
            super(ZoomingSubplotsWithInset, self).on_click(event)

    def zoom(self, selected_ax):
        for ax in self.inset_axes.flat:
            ax.set_visible(False)
        super(ZoomingSubplotsWithInset, self).zoom(selected_ax)
        # restore visibility of the inset_axes corresponding to the zoomed axes
        _, _, i = selected_ax.get_geometry()
        self.inset_axes[i-1].set_visible(True)

    def unzoom(self, selected_ax):
        for ax in self.inset_axes.flat:
            ax.set_visible(True)
        super(ZoomingSubplotsWithInset, self).unzoom(selected_ax)


if __name__ == '__main__':
    N = 10

    subplots = ZoomingSubplotsWithInset(2, 2)

    for ax, axins in zip(subplots.axes.flat, subplots.inset_axes.flat):
        x, y = np.random.random(size=(2, N))
        x2, y2 = np.random.random(size=(2, N))
        ax.plot(x, y)
        axins.plot(x2, y2)

    plt.show()
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65108723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档