def onpick event to plot area inside RectangleSelector !! matplotlib & wxpython

Capture du 2019-04-26 09-33-15.png

hi,

i have app when i use a Rectangle selector to definit some area inside plot the problem is i want to plot area inside rectangelselector , so i need to def onpick event when i click inside rectangle is will be connect to other canvas to plot area inside rectangle i look in this example https://matplotlib.org/users/event_handling.html but i can’t find solution for my idea !!

that my code i need to know when and how i can def onpick event and plot it in other canvas !!

import wx
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.widgets import RectangleSelector
import matplotlib
import matplotlib.pyplot as plt

def line_select_callback(eclick, erelease):
‘eclick and erelease are the press and release events’
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
print("(%3.2f, %3.2f) --> (%3.2f, %3.2f)" % (x1, y1, x2, y2))
print(" The button you used were: %s %s" % (eclick.button, erelease.button))

class Window(wx.Frame):
“”" Fenêtre principale de l’application “”"

def __init__(self, **kwargs):
    super().__init__(None, **kwargs)
    RootPanel(self)

class RootPanel(wx.Panel):
“”" Panel contenant tous les autres widgets de l’application “”"

def __init__(self, parent):
    super().__init__(parent)

    panel_buttons = wx.Panel(self)
    panel_buttons_sizer = wx.GridSizer(1, 2, 0, 0)

    canvas_panel = CanvasPanel(self)

    select_button = PickButton(
        panel_buttons,
        "netCDF4 files (nc)|*.nc",
        canvas_panel.load_from_file,
        label="Show on this window (nc)",
    )
    toplevel_select_button = TopLevelPickButton(
        panel_buttons,
        "Text files (txt)|*.txt|All files|*.*",
        label="Show on separate window (txt)",
    )
    panel_buttons_sizer.Add(select_button)
    panel_buttons_sizer.Add(toplevel_select_button)
    panel_buttons.SetSizer(panel_buttons_sizer)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(panel_buttons)
    sizer.Add(canvas_panel)
    self.SetSizer(sizer)

class PickButton(wx.Button):
“”" Bouton permettant de choisir un fichier “”"

def __init__(self, parent, wildcard, func, **kwargs):
    # func est la méthode à laquelle devra être foruni le fichier sélectionné
    super().__init__(parent, **kwargs)
    self.wildcard = wildcard
    self.func = func
    self.Bind(wx.EVT_BUTTON, self.pick_file)

def pick_file(self, evt):
    style = style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE
    with wx.FileDialog(
        self, "Pick files", wildcard=self.wildcard, style=style
    ) as fileDialog:
        if fileDialog.ShowModal() != wx.ID_CANCEL:
            chosen_file = fileDialog.GetPath()
            self.func(chosen_file)

class TopLevelPickButton(PickButton):
“”" Permet de choisir un fichier et d’ouvrir une toplevel “”"

def __init__(self, parent, wildcard, **kwargs):
    super().__init__(parent, wildcard, self.create_toplevel, **kwargs)

def create_toplevel(self, file_name):
    """ Ouvre une toplevel et affiche le graphique """
    self.win = TopLevelCanvas(self.Parent)
    self.win.canvas_panel.load_from_file(file_name)
    self.win.Show()

class CanvasPanel(wx.Panel):
“”" Panel du graphique matplotlib “”"
def init(self, parent , size=(200,250)):
super().init(parent)
self.figure = Figure(figsize =(8,7))
self.canvas = FigureCanvas(self, -1, self.figure)
self.Size = self.canvas.Size

def load_from_file(self, file_name):
    """
    Méthode effectuant l'intermédiaire pour charger le fichier selon
    son type
    """
    self.axes = self.figure.add_subplot(111)
    if file_name.endswith(".nc"):
        self._load_nc(file_name)
    else:
        self._load_txt(file_name)
    self.canvas.draw()

def _load_nc(self, file_name):
    """ Simule le chargement et affichage à partir d'un fichier nc """
    t = np.arange(0.0, 8.0, 0.01)
    s = np.sin(3 * np.pi * t)
    self.axes.plot(t, s)
 


    RS = RectangleSelector(self.axes, line_select_callback,
                                   drawtype='box', useblit=True,
                                   button=[1],  
                                   minspanx=5, minspany=5,
                                   spancoords='pixels',
                                   interactive=True)
    self.RS = RectangleSelector(self.axes,line_select_callback,
                                   drawtype='box', useblit=False,
                                   button=[1, 3],minspanx=5, minspany=5,
                                   spancoords='pixels',
                                   interactive=True, rectprops = dict(facecolor='None',edgecolor='red',alpha=5,fill=False))
   
   
    self.RS.to_draw.set_visible(True)
    self.RS.extents = (0,1,0,1)
    self.figure.canvas.draw()

class TopLevelCanvas(wx.Frame):
“”" Fenêtre affichant uniquement un graph matplotlib “”"

def __init__(self, parent, **kwargs):
    super().__init__(parent, **kwargs)
    self.canvas_panel = CanvasPanel(self)
    self.Size = self.canvas_panel.Size

class App(wx.App):
def OnInit(self):
win = Window(title=“A test dialog”, size=(1000, 800))
win.Show()
return True

if name == “main”:
app = App()
app.MainLoop()

``

thank you

Majda