簡體   English   中英

單擊時放大子圖(帶有 inset_axes 圖)

[英]Zoom in on subplot (with inset_axes plot) on click

所以我遵循了這個線程的方法:

我怎樣才能在 plot 中獨立和在 Matplotlib 的子圖中使用相同的圖?

正如他的示例所示,它實際上工作得很好。 但是,一個問題是我的圖中有inset_axes圖。 因此,當它放大時,所有插圖都保留下來,實際上與放大的 plot 重疊。

但是,我不確定如何刪除它們,甚至可能還放大與單擊的子圖一起的插圖 plot。

所以從線程我剛剛使用這個 class 進行縮放方法:

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

然后當我 plot 我使用這個:

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

但如上所述,所有子圖的插圖 plot 將保留在其 position 中,並與放大的 plot 重疊。 我怎樣才能改變它,這樣他們就不會干預,甚至可能子圖的 inset_plot 仍然存在並且也被放大了?

我找不到更改 inset_axes 的inset_axes的方法,可能是因為它們的 position 和大小被鎖定到它們的父軸。

同時,這是一個新的 class,它會自動創建 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM