我在wxpython中有第一个代码
在第二个代码中,当我用鼠标单击任何地方时,矩形补丁都会移动
第一个代码:
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):
wx.Panel.__init__(self,parent,size = (300,300))
self.SetBackgroundColour('white')
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 | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
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.01,1,0.1)
def line_select_callback(self, eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
rect = self.RS.artists[0] # Rectangle artist
slave_rect = self.Parent.Bottom.RS.artists[0] # Rectangle artist
slave_canvas = self.Parent.Bottom.canvas
# Force new positional values into slave rectangle
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))
#Force visible in case slave rectangle has been clicked on
slave_rect.set_visible(True)
#Redraw slave rectangle
slave_canvas.draw()
class PanelBottom(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self, parent, 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 | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
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=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
self.GrandParent.zoom.bottom.Update(self.zoom_axes)
app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()
第二个代码:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def on_press(event):
xpress, ypress = event.xdata, event.ydata
w = rect.get_width()
h = rect.get_height()
rect.set_xy((xpress-w/2, ypress-h/2))
ax.lines = []
ax.axvline(xpress, c='b')
ax.axhline(ypress, c='b')
fig.canvas.draw()
x = y = 1
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
fig.canvas.mpl_connect('button_press_event', on_press)
rect = patches.Rectangle((x,y), 0.2,0.2, alpha=1, fill=None, label='Label')
ax.add_patch(rect)
plt.show()
我需要做的是在第一个代码上应用矩形补丁,然后用鼠标移动它,但是同时在第二个图上移动,就像我有两个矩形补丁,但是同时移动和移动时一样鼠标在第一个图中绘制矩形路径,第二个同时绘制
我是matplotlib中的新手
先谢谢您