我有两个带有2个矩形的面板,它们在同一区域上同时移动。我想通过只单击第一个面板中的矩形并在2上自动显示缩放后,在两个面板上可视化所选区域。在其他面板上,单击矩形可以可视化相同区域,但仅用于第一个绘图 当我选择区域并单击第一个矩形时,如何同时绘制2个图的缩放比例 我该怎么做
该示例代码:
import wx
from numpy import arange, sin, pi,cos
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.widgets import RectangleSelector
from matplotlib.figure import Figure
class MainFrame(wx.Frame):
def __init__(self, parent ):
wx.Panel.__init__(self, parent,name="Main", size = (600,800))
self.Top = PanelTop(self)
self.Bottom = PanelBottom(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.Top, 1,wx.EXPAND)
sizer.Add(self.Bottom, 1,wx.EXPAND)
self.SetSizer(sizer)
class PanelTop(wx.Panel):
def __init__(self,parent):
super().__init__(parent)
panel = wx.Panel(self)
self.zoom_panel = Zoom(parent=self)
self.canvas_panel = CanvasPanel(self)
canvas_sizer = wx.BoxSizer(wx.HORIZONTAL)
canvas_sizer.Add(self.canvas_panel,1,wx.EXPAND)
canvas_sizer.Add(self.zoom_panel,1,wx.EXPAND)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel)
sizer.Add(canvas_sizer)
self.SetSizerAndFit(sizer)
self.Show()
class CanvasPanel(wx.Panel):
""" Panel du graphique matplotlib """
def __init__(self, parent , size=(200,250)):
super().__init__(parent)
self.figure = Figure(figsize =(4,3))
self.canvas = FigureCanvas(self, -1, self.figure)
self.axes = self.figure.add_subplot(111)
self.Size = self.canvas.Size
self.parent = parent
t = arange(0.5, 3.0, 0.01)
s = cos(2 * pi * t)
self.axes.plot(t, s)
self.RS = RectangleSelector(self.axes,self.line_select_callback,
drawtype='box', useblit=False,
button=[1, 3],minspanx=1, minspany=1,
spancoords='pixels',
interactive=True , rectprops = dict(facecolor='None',edgecolor='green',alpha=5,fill=False))
self.RS.to_draw.set_visible(True)
self.RS.extents = (1,0.1,1,0.1)
def line_select_callback(self, eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
self.zoom_axes=[x1,x2,y1,y2]
self.parent.zoom_panel.Update(self)
rect = self.RS.artists[0] # Rectangle artist
slave_rect = self.GrandParent.Bottom.RS.artists[0] # Rectangle artist
slave_canvas = self.GrandParent.Bottom.canvas
slave_rect.set_x(x1)
slave_rect.set_y(y1)
slave_rect.set_width(x2-x1)
slave_rect.set_height(y2-y1)
slave_rect.update(dict(facecolor='None',edgecolor='green',alpha=5,fill=False))
slave_rect.set_visible(True)
slave_canvas.draw()
self.zoom_axes=[x1,x2,y1,y2]
self.parent.zoom_panel.Update(self)
class Zoom(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent,size=(200,250))
self.Show()
def Update(self,parent):
zoom_axes=parent.zoom_axes
self.figure = Figure(figsize =(4,3))
self.canvas = FigureCanvas(self, -1, self.figure)
self.axes = self.figure.add_subplot(111)
self.axes.axis(zoom_axes)
t = arange(0.5, 3.0, 0.01)
s = cos(2 * pi * t)
self.axes.plot(t, s)
self.canvas.draw()
self.Refresh()
class PanelBottom(wx.Panel):
def __init__(self,parent):
super().__init__( parent, name = "Bottom", size = (300,300))
self.SetBackgroundColour('grey77')
self.figure = Figure(figsize = (4,5))
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self,-1,self.figure)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT )
self.SetSizer(self.sizer)
t = arange(0.5, 3.0, 0.01)
s = cos(5 * pi * t)
self.axes.plot(t, s)
self.RS = RectangleSelector(self.axes,self.line_select_callback,
drawtype='box', useblit=False,
button=[1, 3],minspanx=1, minspany=1,
spancoords='pixels',
interactive=False, rectprops = dict(facecolor='None',edgecolor='green',alpha=5,fill=False))
self.RS.to_draw.set_visible(True)
self.RS.extents = (1,0,0,1)
def line_select_callback(self, eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()