wxpython and matplotlib graph GUI compatibility

by Shubham Shaw   Last Updated June 12, 2019 19:24 PM

Below code, I am using to position my graph in the panel, but the positioning of the graph is not happening properly(please refer attached images, I wanted to position inside the black panel):

# -*- coding: utf-8 -*-
#

Python code generated with wxFormBuilder (version Mar 23 2019)

http://www.wxformbuilder.org/

#

PLEASE DO NOT EDIT THIS FILE!

#

import wx import wx.xrc import wx.adv from wx.lib.plot import PolyLine, PlotCanvas, PlotGraphics,PolyMarker import numpy

import matplotlib as mpl from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar from wxmplot import PlotPanel

#

Class MainFrame

#

class MainFrame ( wx.Frame ): def init( self, parent ): #main frame wx.Frame.init ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 728,467 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL ) self.Maximize( True) self.SetSizeHints( wx.DefaultSize, wx.DefaultSize ) self.SetBackgroundColour( wx.Colour( 208, 208, 208 ) ) #Creating First sizer mainSizer = wx.BoxSizer( wx.VERTICAL )

    #Creating first sizer on the vertical sizer mainSizer
    firstSizer = wx.FlexGridSizer( 0, 2, 0, 0 )
    firstSizer.SetFlexibleDirection( wx.BOTH )
    firstSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )

    #creating tabs in the first sizer
    self.Tabs = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( 400,500 ), 0 )

    #creating page(report generator page) for the first tab
    self.Report_Generator = wx.Panel( self.Tabs, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
    reportGeneratorSizer = wx.FlexGridSizer( 0, 3, 0, 0 )
    reportGeneratorSizer.SetFlexibleDirection( wx.BOTH )
    reportGeneratorSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
    self.Report_Generator.SetSizer( reportGeneratorSizer )
    self.Report_Generator.Layout()
    reportGeneratorSizer.Fit( self.Report_Generator )
    self.Tabs.AddPage( self.Report_Generator, u"Report Generator", False )

    # creating page(Graph generator page) for the second tab
    self.Graph_Generator = wx.Panel( self.Tabs, wx.ID_ANY, wx.DefaultPosition, wx.Size( 50,-1 ), wx.TAB_TRAVERSAL )
    GraphGeneratorSizer = wx.FlexGridSizer( 0, 3, 0, 0 )
    GraphGeneratorSizer.SetFlexibleDirection( wx.BOTH )
    GraphGeneratorSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
    self.Graph_Generator.SetSizer( GraphGeneratorSizer )
    self.Graph_Generator.Layout()
    self.Tabs.AddPage( self.Graph_Generator, u"Graph Generator", True )

    firstSizer.Add( self.Tabs, 1, wx.EXPAND |wx.ALL, 5 )

    self.Graph_section = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( 1000,700 ), wx.TAB_TRAVERSAL )
    #self.Graph_section.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_WINDOW ) )
    self.Graph_section.SetBackgroundColour('black')

    DrawGraphSizer = wx.BoxSizer(wx.VERTICAL)
    DrawGraphHorSizer = wx.BoxSizer(wx.HORIZONTAL)

    self.figure = mpl.figure.Figure(dpi = None, figsize=None)
    self.figure.add_subplot(111).plot([1, 2, 3], [2, 1, 4])
    self.canvas = FigureCanvas(self,-1,self.figure)
    #self.toolbar =  NavigationToolbar(self.canvas)
    #self.toolbar.Realize()
    DrawGraphHorSizer.Add( ( 0, 600), 1, wx.EXPAND, 5 )
    DrawGraphHorSizer.Add( ( 0, 600), 1, wx.EXPAND, 5 )

    DrawGraphHorSizer.Add(self.canvas,1,wx.EXPAND)

    #DrawGraphSizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)

    self.Graph_section.SetSizer(DrawGraphHorSizer)

    #firstSizer.Add( self.Graph_section, 1, wx.EXPAND |wx.ALL, 0 )
    firstSizer.Add( self.Graph_section, 1, wx.EXPAND |wx.ALL, 0 )

    mainSizer.Add( firstSizer, 1, wx.ALL, 0 )

    self.SetSizer( mainSizer )
    self.Layout()
    self.menuBar = wx.MenuBar( 0 )
    self.menuBar.SetForegroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_MENU ) )
    self.menuBar.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_MENU ) )

    self.File = wx.Menu()
    self.menuBar.Append( self.File, u"File" )

    self.Help = wx.Menu()
    self.about = wx.MenuItem( self.Help, wx.ID_ANY, u"About...", wx.EmptyString, wx.ITEM_NORMAL )
    self.Help.Append( self.about )

    self.menuBar.Append( self.Help, u"Help" )

    self.SetMenuBar( self.menuBar )

    self.Centre( wx.BOTH )

def __del__( self ):
    pass
#

Below written code is responsible for creating the executing the code for the GUI

#

app = wx.App() frame = MainFrame(None) frame.Show() app.MainLoop()

How it looks like.[![][1]]2

Tags : discussion bug


Related Questions


Updated March 26, 2015 07:30 AM

Updated March 26, 2015 07:30 AM

Updated March 27, 2015 05:08 AM

Updated January 03, 2018 07:24 AM

Updated March 29, 2015 06:08 AM