【发布时间】: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