【问题标题】:Zoom in on subplot (with inset_axes plot) on click单击时放大子图(带有 inset_axes 图)
【发布时间】:2021-03-14 10:48:15
【问题描述】:

所以我遵循了这个线程的方法:

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

而且它实际上运作良好,如他的示例所示。但是,一个问题是我的地块中有inset_axes 地块。所以当它放大时,所有的插图都保留下来,实际上与放大的图重叠。

但是我不确定如何删除它们,甚至可能还放大与被点击的子图一起的插图。

所以从线程中我刚刚使用了这个类进行缩放方法:

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()

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

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 仍然存在并且也被放大了?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    我找不到更改inset_axes 位置的方法,可能是因为它们的位置和大小已锁定到它们的父轴。

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

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2020-12-03
      • 1970-01-01
      • 2018-03-03
      相关资源
      最近更新 更多