I am trying to put a cursor and rectangle patch that can moved and that can display the coordinates or rather pixel of an image select and display on another panel Paneltwo in the textctrl each time I move the mouse ( automatically rectangel) the pixels of each point are displayed on the other panel
the first problem that the rectangle can't moved with mouse ! the second that what i need to do is when the rectangle moved with mouse i can visualize the pixel or position of mouse ( rectangle) in panel two textctrl ! how can I do that ?
that part of code :
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,400))
self.Panel = Panel(self)
class Panel(wx.Panel):
def __init__(self,parent):
super().__init__(parent)
panel = wx.Panel(self)
self.panel_two = PanelTwo(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.panel_two,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)
#can'tmove rectangel with mouse
def on_press(self,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')
self.fig.canvas.draw()
self.fig = plt.figure
self.axes = plt.subplot(111)
self.axes.imshow(t,s)
self.fig.canvas.mpl_connect('button_press_event',on_press)
self.rect = patches.Rectangle((x,y),0.01,0.01,linewidth=1,edgecolor='g',facecolor='none')
self.axes.add_patch(rect)
self.plt.show()
class PanelTwo(wx.Panel): #here when i need to visualize pixel and coordinator cursor
def __init__(self,parent):
wx.Panel.__init__(self,parent,size=(200,250))
self.text_ctrl = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_READONLY|
wx.TE_RICH2, size=(200,170), pos = (40,20))
lbl = wx.StaticText(self,label="Coordinato cursor & Pixel " , pos=(40,0))
app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()
THANK YOU
Try this and see if it moves you in the right direction.
Note: I have no idea how you are going to retrieve the pixel position from that plot.
Perhaps someone else, who actually knows what they are doing with matplotlib, because I certainly don't, can help with that.
import wx
from numpy import arange, sin, pi,cos
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.patches as patches
class MainFrame(wx.Frame):
def __init__(self, parent ):
wx.Panel.__init__(self, parent,name="Main", size = (800,400))
self.Panel = Panel(self)
class Panel(wx.Panel):
def __init__(self,parent):
super().__init__(parent)
panel = wx.Panel(self)
self.panel_two = PanelTwo(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.panel_two,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.canvas.mpl_connect('button_press_event', self.on_press)
x = y = 0.2
self.rect = patches.Rectangle((x, y), 0.4, 0.4, alpha=1, fill=None, label='Label')
self.axes.add_patch(self.rect)
self.axes.plot()
def on_press(self, click):
x1, y1 = click.xdata, click.ydata
self.parent.panel_two.Update(x1,y1)
self.rect.set_x(x1 - 0.2) #Move the rectangle and centre it on the X click point
self.rect.set_y(y1 - 0.2) #Move the rectangle and centre it on the Y click point
self.axes.plot()
self.canvas.draw()
class PanelTwo(wx.Panel): #here when i need to visualize pixel and coordinator cursor
def __init__(self,parent):
wx.Panel.__init__(self,parent,size=(300,250))
self.text_ctrl = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.BORDER_SUNKEN|wx.TE_READONLY|wx.TE_RICH2, size=(200,170))
lbl = wx.StaticText(self,label="Coordinato cursor & Pixel ")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(lbl,0, wx.ALIGN_CENTRE,10)
sizer.Add(self.text_ctrl,0, wx.ALIGN_CENTRE,10)
self.SetSizer(sizer)
def Update(self,x1,y1):
self.text_ctrl.SetValue("Mouse click at;\nX "+str(x1)+"\nY "+str(y1))
app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()