= How to use Plot - Part 2 (Phoenix) = '''Keywords :''' [[PyPlot|Plot]], Graph, Point, Sine, Cosine, PolyLine, PlotCanvas, PlotGraphics. <<TableOfContents>> -------- = Demonstrating : = __'''''Tested''' py3.x, wx4.x and Win10. ''__ Are you ready to use some samples ? ;) Test, modify, correct, complete, improve and share your discoveries ! (!) -------- == Plot : == === First example === {{attachment:img_sample_one.png}} {{{#!python # sample_one.py """ Using wxPython for plotting Author : Vegaseat Created : 19 apr 2007 Updated : Octo. 19, 2020 by Ecco Link : https://www.daniweb.com/programming/software-development/code/216913/using-wxpython-for-plotting """ import os import sys import wx import wx.lib.plot as plot # class MyFrame # class MyApp #--------------------------------------------------------------------------- class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, title="wx.lib.plot") #------------ # Return icons folder. self.icons_dir = wx.GetApp().GetIconsDir() #------------ # Add a panel so it looks the correct on all platforms. panel = wx.Panel(self) panel.SetBackgroundColour("white") #------------ self.plotter = plot.PlotCanvas(panel) self.plotter.SetInitialSize(size=(410, 340)) # Enable the zoom feature (drag a box around area of interest). # self.plotter.SetEnableZoom(True) toggleZoom = wx.CheckBox(panel, label="Show zoom") toggleZoom.SetValue(False) toggleZoom.Bind(wx.EVT_CHECKBOX, self.OnToggleZoom) #------------ # Create some sizers. mainSizer = wx.BoxSizer(wx.VERTICAL) checkSizer = wx.BoxSizer(wx.HORIZONTAL) # Layout the widgets. mainSizer.Add(self.plotter, 1, wx.EXPAND | wx.ALL, 10) checkSizer.Add(toggleZoom, 0, wx.ALL, 5) mainSizer.Add(checkSizer) panel.SetSizer(mainSizer) #------------ # List of (x,y) data point tuples. data = [(1, 2), (2, 3), (3, 5), (4, 6), (5, 8), (6, 8), (12, 10), (13, 4)] # Draw points as a line. line = plot.PolyLine(data, colour='red', width=1) # Also draw markers, default colour is black and size is 2. # Other shapes 'circle', 'cross', 'square', 'dot', 'plus'. marker = plot.PolyMarker(data, marker='triangle') # Set up text, axis and draw. gc = plot.PlotGraphics([line, marker], 'Line/Marker graph', 'X axis', 'Y axis') self.plotter.Draw(gc, xAxis=(0, 15), yAxis=(0, 15)) #------------ # Simplified init method. self.SetProperties() #----------------------------------------------------------------------- def SetProperties(self): """ ... """ self.SetMinSize((410, 340)) self.SetBackgroundColour(wx.WHITE) #------------ frameIcon = wx.Icon(os.path.join(self.icons_dir, "wxwin.ico"), type=wx.BITMAP_TYPE_ICO) self.SetIcon(frameIcon) def OnToggleZoom(self, event): """ Enable the zoom feature (drag a box around area of interest). """ # self.canvas.SetEnableZoom(event.IsChecked()) self.plotter.enableZoom = event.IsChecked() #--------------------------------------------------------------------------- class MyApp(wx.App): def OnInit(self): #------------ self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0] #------------ frame = MyFrame() self.SetTopWindow(frame) frame.Show(True) return True #----------------------------------------------------------------------- def GetInstallDir(self): """ Return the installation directory for my application. """ return self.installDir def GetIconsDir(self): """ Return the icons directory for my application. """ icons_dir = os.path.join(self.installDir, "icons") return icons_dir #--------------------------------------------------------------------------- def main(): app = MyApp(False) app.MainLoop() #--------------------------------------------------------------------------- if __name__ == "__main__" : main() }}} -------- === Second example === {{attachment:img_sample_two.png}} {{{#!python # sample_two.py """ Author : Beppe Created : Sept. 22, 2010 Updated : Octo. 21, 2020 by Ecco Link : https://www.blog.pythonlibrary.org/2010/09/27/wxpython-pyplot-graphs-with-python/ """ import os import sys import wx import wx.lib.plot as plot # class MyPLot # class MyForm # class MyApp #--------------------------------------------------------------------------- class MyPLot(plot.PlotCanvas): def __init__(self, parent, id): plot.PlotCanvas.__init__(self, parent, -1) #----------------------------------------------------------------------- def PlotGraph(self): """ ... """ # self.SetEnableLegend(True) # self.SetEnableGrid(True) #------------ min_value = [(1, 3), (2, 3), (3, 6), (4, 8), (5, 13), (6, 17), (7, 19), (8, 19), (9, 16), (10, 12), (11, 8), (12, 4)] max_value = [(1, 13), (2, 14), (3, 17), (4, 19), (5, 24), (6, 29), (7, 32), (8, 32), (9, 27), (10, 23), (11, 17), (12, 13)] #------------ min_line = plot.PolyLine(min_value, legend="Lowest", colour='blue', width=1) min_marker = plot.PolyMarker(min_value, marker='triangle_down') max_line = plot.PolyLine(max_value, legend="Maximum", colour='orange', width=1) max_marker = plot.PolyMarker(max_value, marker='triangle') #------------ gc = plot.PlotGraphics([min_line, min_marker, max_line, max_marker], "Average temperature graph for Rome", 'Months', 'Temperatures') self.Draw(gc, xAxis=(0, 13), yAxis=(0, 35)) def _xticks(self, *args): """ Override _xticks to write months on x axis. """ ticks = plot.PlotCanvas._xticks(self, *args) new_ticks = [] month = ("",'Jan','Feb','Mar','Apr','May','Jiu', 'Jul','Aug','Sep','Ott','Nov','Dec') for k,v in enumerate(month): s = str(month[int(k)]) new_ticks.append((k, s)) return new_ticks def _yticks(self, *args): """ Override _yticks to write centigrade on y axis. """ ticks = plot.PlotCanvas._yticks(self, *args) new_ticks = [] for tick in ticks: t = tick[0] s = str(" %s °C" %t) new_ticks.append( (t, s) ) return new_ticks #--------------------------------------------------------------------------- class MyForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, title="Axis marks", size=(600, 400)) #------------ # Return icons folder. self.icons_dir = wx.GetApp().GetIconsDir() #------------ panel = wx.Panel(self) self.canvas = MyPLot(panel, -1) toggleGrid = wx.CheckBox(panel, label="Show Grid") toggleGrid.SetValue(True) toggleGrid.Bind(wx.EVT_CHECKBOX, self.OnToggleGrid) toggleLegend = wx.CheckBox(panel, label="Show Legend") toggleLegend.SetValue(False) toggleLegend.Bind(wx.EVT_CHECKBOX, self.OnToggleLegend) #------------ mainSizer = wx.BoxSizer(wx.VERTICAL) checkSizer = wx.BoxSizer(wx.HORIZONTAL) mainSizer.Add(self.canvas, 1, wx.EXPAND | wx.ALL, 10) checkSizer.Add(toggleGrid, 0, wx.ALL, 5) checkSizer.Add(toggleLegend, 0, wx.ALL, 5) mainSizer.Add(checkSizer) panel.SetSizer(mainSizer) self.Layout() #------------ # Simplified init method. self.SetProperties() #----------------------------------------------------------------------- def SetProperties(self): """ ... """ self.SetMinSize((600, 400)) self.SetBackgroundColour(wx.WHITE) #------------ frameIcon = wx.Icon(os.path.join(self.icons_dir, "wxwin.ico"), type=wx.BITMAP_TYPE_ICO) self.SetIcon(frameIcon) def OnToggleGrid(self, event): """ ... """ # self.canvas.SetEnableGrid(event.IsChecked()) self.canvas.enableGrid = event.IsChecked() def OnToggleLegend(self, event): """ ... """ # self.canvas.SetEnableLegend(event.IsChecked()) self.canvas.enableLegend = event.IsChecked() def OnOpen(self): """ ... """ self.canvas.PlotGraph() #--------------------------------------------------------------------------- class MyApp(wx.App): def OnInit(self): #------------ self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0] #------------ frame = MyForm() frame.OnOpen() self.SetTopWindow(frame) frame.Show(True) return True #----------------------------------------------------------------------- def GetInstallDir(self): """ Return the installation directory for my application. """ return self.installDir def GetIconsDir(self): """ Return the icons directory for my application. """ icons_dir = os.path.join(self.installDir, "icons") return icons_dir #--------------------------------------------------------------------------- def main(): app = MyApp(False) app.MainLoop() #--------------------------------------------------------------------------- if __name__ == "__main__" : main() }}} -------- === Third example === {{attachment:img_sample_three.png}} {{{#!python # sample_three.py """ Using wxPython for plotting Author : Vegaseat Created : 19 apr 2007 Updated : Octo. 19, 2020 by Ecco Link : https://www.daniweb.com/programming/software-development/code/216913/using-wxpython-for-plotting """ import os import sys import wx import wx.lib.plot as plot # class MyFrame # class MyApp #--------------------------------------------------------------------------- class MyFrame(wx.Frame): def __init__(self): style = wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX) wx.Frame.__init__(self, None, wx.ID_ANY, title="wx.lib.plot", size=(500, 340), style=style) #------------ # Return icons folder. self.icons_dir = wx.GetApp().GetIconsDir() #------------ # Add a panel so it looks the correct on all platforms. self.panel = wx.Panel(self) self.panel.SetBackgroundColour("white") #------------ self.button = wx.Button(self.panel, -1, "&Update", (200, 265)) self.button.Bind(wx.EVT_BUTTON, self.OnRedraw) self.plotter = plot.PlotCanvas(self.panel) self.plotter.SetInitialSize(size=(500, 250)) #------------ # List of (x,y) data point tuples. data = [[1, 10], [2, 5], [3, 10], [4, 5]] line = plot.PolyLine(data, colour='red', width=1) gc = plot.PlotGraphics([line], 'Line graph', 'x', 'y') self.plotter.Draw(gc) #------------ # Simplified init method. self.SetProperties() #----------------------------------------------------------------------- def SetProperties(self): """ ... """ self.SetMinSize((500, 340)) self.SetBackgroundColour(wx.WHITE) #------------ frameIcon = wx.Icon(os.path.join(self.icons_dir, "wxwin.ico"), type=wx.BITMAP_TYPE_ICO) self.SetIcon(frameIcon) def OnRedraw(self, event): """ ... """ self.plotter = plot.PlotCanvas(self.panel) self.plotter.SetInitialSize(size=(500, 250)) data2 = [[1, 20], [2, 15], [3, 20], [4, -10]] line = plot.PolyLine(data2, colour='red', width=1) gc = plot.PlotGraphics([line], 'Line graph', 'x', 'y') self.plotter.Draw(gc) #--------------------------------------------------------------------------- class MyApp(wx.App): def OnInit(self): #------------ self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0] #------------ frame = MyFrame() self.SetTopWindow(frame) frame.Show(True) return True #----------------------------------------------------------------------- def GetInstallDir(self): """ Return the installation directory for my application. """ return self.installDir def GetIconsDir(self): """ Return the icons directory for my application. """ icons_dir = os.path.join(self.installDir, "icons") return icons_dir #--------------------------------------------------------------------------- def main(): app = MyApp(False) app.MainLoop() #--------------------------------------------------------------------------- if __name__ == "__main__" : main() }}} -------- = Download source = [[attachment:source.zip]] -------- = Additional Information = '''Link :''' https://www.blog.pythonlibrary.org/2010/09/27/wxpython-pyplot-graphs-with-python/ - - - - - https://wiki.wxpython.org/TitleIndex https://docs.wxpython.org/ -------- = Thanks to = Vegaseat (sample_one / three.py coding), Beppe (sample_two.py coding), Daniweb.com, the wxPython community... -------- = About this page = Date (d/m/y) Person (bot) Comments : 22/10/20 - Ecco (Created page for wxPython Phoenix). -------- = Comments = - blah, blah, blah....