hi ,
i have my app when i click button to select netcdf4 my file netcdf have a variable named IR when i show the variable in panel the figure is show in other panel out of panel
how can i fixe this problem cause i need to show my figure in the same panel !?
that my code and that result :
import wx
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
import os
import netCDF4
from netCDF4 import Dataset
import matplotlib.pyplot as plt
class Window(wx.Frame):
“”" principal “”"
def __init__(self, **kwargs):
super().__init__(None, **kwargs)
RootPanel(self)
class RootPanel(wx.Panel):
“”" Panel for widgets app “”"
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.SetSizerAndFit(sizer)
class PickButton(wx.Button):
“”" Bouton choose file “”"
def __init__(self, parent, wildcard, func, **kwargs):
# func for choosing file
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):
“”" to choose and open toplevel “”"
def __init__(self, parent, wildcard, **kwargs):
super().__init__(parent, wildcard, self.create_toplevel, **kwargs)
def create_toplevel(self, file_name):
""" open toplevel and show figure """
self.win = TopLevelCanvas(self.Parent)
self.win.canvas_panel.load_from_file(file_name)
self.win.Show()
class CanvasPanel(wx.Panel):
“”" Panel matplotlib “”"
def __init__(self, parent):
super().__init__(parent)
self.figure = Figure()
self.canvas = FigureCanvas(self, -1, self.figure)
self.Size = self.canvas.Size
def load_from_file(self, file_name):
self.axes = self.figure.add_subplot(111)
self.axes = self.figure.add_axes([0,0,1,1])
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):
""" open and show file netCDF4 """
path='`/home/2019/03/25/Mmul_201903250000.nc`'
nc = netCDF4.Dataset('/home/2019/03/25/Mmul_201903250000.nc')
IR=nc.variables['IR_'][:]
#Affichage de la variable
plt.pcolormesh(IR)
plt.colorbar()
plt.show()
def _load_txt(self, file_name):
""" open and show txt """
class TopLevelCanvas(wx.Frame):
“”" panel for show 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=(800, 600))
win.Show()
return True
if name == “main”:
app = App()
app.MainLoop()
``
how can i read file netcdf4 and show it in my app not out app
thank u for help