= How to use Plot - Part 3 (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 ! (!) -------- == Plotting : == wxPython comes with a simple plotting library. Features include zooming, legends and a grid. Possible graphs : * Scatter * Line * Bar We must install the Numeric library. Other plotting libraries are : {{{Gnuplot}}} or {{{MatPlotLib}}}. These must be installed separately. To create a graph, we follow these steps : '''1)''' Define our data : We insert your data in a list of tuples. Each tuple will have two items. data = [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x5, y5), (x6, y6)] '''2)''' Create a plotting canvas we create an object of a {{{PlotCanvas}}} as a child of a frame : {{{ frame = wx.Frame(self, -1) client = wx.lib.plot.PlotCanvas(frame) }}} '''3)''' Create a graph There are two classes : {{{PolyLine}}} and {{{PolyMarker}}}. {{{PolyLine}}} class defines line graphs. Its constructor is : {{{ PolyLine(list data, wx.Colour colour, integer width, integer style, string legend) }}} ''- data'' parameter is the data to be displayed. ''- colour'' defines the colour of the line. ''- width'' is the width of the pen, used to draw the graph. - Possible ''style'' flags are wx.Pen styles. ''- legend'' defines the line legend. {{{PolyMarker}}} can be used to create scatter graphs and bar graphs as well. Constructor : {{{ PolyMarker(list data, wx.Colour colour, integer size, wx.Colour fillcolour, integer fillstyle, string markershape, string legend) }}} ''fillstyle'' is also various wx.Pen styles. Marker Shapes : * circle * dot * square * triangle * triangle_down * cross * plus '''4)''' Create a graph container : Graph container is a container that holds a graph object and its title and labels. {{{ PlotGraphics(list objects, string title, string xLabel, string yLabel) }}} * objects is a list of one or more graph objects * title - title shown at top of graph * xLabel - label shown on x-axis * yLabel - label shown on y-axis '''5)''' Draw a graph : Finally we draw the graph. {{{ client.Draw(gc, xAxis=(0,15), yAxis=(0,15)) }}} gc is a graph container object. xAxis and yAxis define the range of the axes (info by '''ZetCode / Jan Bodnar'''). === First example === {{attachment:img_sample_one.png}} {{{#!python # sample_one.py """ Author : Zetcode Created : Apr., 2007 Updated : Nov. 16, 2020 by Ecco """ import wx import wx.lib.plot as plot # MyPlot # MyApp #--------------------------------------------------------------------------- class MyPlot(wx.Dialog): def __init__(self, parent, id, title): wx.Dialog.__init__(self, parent, id, title, size=(180, 280)) #------------ icon = wx.Icon("./icons/wxwin.ico") self.SetIcon(icon) #------------ self.data = [(1,2), (2,3), (3,5), (4,6), (5,8), (6,8), (10,10)] btn1 = wx.Button(self, 1, '&Scatter', (50,50)) btn2 = wx.Button(self, 2, '&Line', (50,90)) btn3 = wx.Button(self, 3, '&Bar', (50,130)) btn4 = wx.Button(self, 4, '&Quit', (50,170)) #------------ self.Bind(wx.EVT_BUTTON, self.OnScatter, id=1) self.Bind(wx.EVT_BUTTON, self.OnLine, id=2) self.Bind(wx.EVT_BUTTON, self.OnBar, id=3) self.Bind(wx.EVT_BUTTON, self.OnQuit, id=4) self.Bind(wx.EVT_CLOSE, self.OnQuit) #----------------------------------------------------------------------- def OnScatter(self, event): frm = wx.Frame(self, -1, 'Scatter', size=(600, 450)) icon = wx.Icon("./icons/wxwin.ico") frm.SetIcon(icon) pnl = wx.Panel(frm, -1) pnl.SetBackgroundColour(wx.WHITE) #------------ client = plot.PlotCanvas(pnl) markers = plot.PolyMarker(self.data, legend='', colour='pink', marker='triangle_down', size=1) gc = plot.PlotGraphics([markers], 'Scatter Graph', 'X Axis', 'Y Axis') client.Draw(gc, xAxis=(0,15), yAxis=(0,15)) #------------ mainSizer = wx.BoxSizer(wx.VERTICAL) mainSizer.Add(client, 1, wx.EXPAND | wx.ALL, 10) pnl.SetSizer(mainSizer) #------------ frm.Show(True) def OnLine(self, event): frm = wx.Frame(self, -1, 'Line', size=(600, 450)) icon = wx.Icon("./icons/wxwin.ico") frm.SetIcon(icon) pnl = wx.Panel(frm, -1) pnl.SetBackgroundColour(wx.WHITE) #------------ client = plot.PlotCanvas(pnl) line = plot.PolyLine(self.data, legend='', colour='pink', width=1) gc = plot.PlotGraphics([line], 'Line Graph', 'X Axis', 'Y Axis') client.Draw(gc, xAxis= (0,15), yAxis= (0,15)) #------------ mainSizer = wx.BoxSizer(wx.VERTICAL) mainSizer.Add(client, 1, wx.EXPAND | wx.ALL, 10) pnl.SetSizer(mainSizer) #------------ frm.Show(True) def OnBar(self, event): frm = wx.Frame(self, -1, 'Bar', size=(600, 450)) icon = wx.Icon("./icons/wxwin.ico") frm.SetIcon(icon) pnl = wx.Panel(frm, -1) pnl.SetBackgroundColour(wx.WHITE) #------------ client = plot.PlotCanvas(pnl) bar1 = plot.PolyLine([(1, 0), (1,5)], legend='', colour='gray', width=25) bar2 = plot.PolyLine([(3, 0), (3,8)], legend='', colour='gray', width=25) bar3 = plot.PolyLine([(5, 0), (5,12)], legend='', colour='gray', width=25) bar4 = plot.PolyLine([(6, 0), (6,2)], legend='', colour='gray', width=25) gc = plot.PlotGraphics([bar1, bar2, bar3, bar4],'Bar Graph', 'X Axis', 'Y Axis') client.Draw(gc, xAxis=(0,15), yAxis=(0,15)) #------------ mainSizer = wx.BoxSizer(wx.VERTICAL) mainSizer.Add(client, 1, wx.EXPAND | wx.ALL, 10) pnl.SetSizer(mainSizer) #------------ frm.Show(True) def OnQuit(self, event): self.Destroy() #--------------------------------------------------------------------------- class MyApp(wx.App): def OnInit(self): dlg = MyPlot(None, -1, 'Plot.py') dlg.Show(True) dlg.Centre() return True #--------------------------------------------------------------------------- app = MyApp(0) app.MainLoop() }}} -------- = 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 = Jan Bodnar / ZetCode (sample_one.py coding), the wxPython community... -------- = About this page = Date (d/m/y) Person (bot) Comments : 16/11/20 - Ecco (Created page for wxPython Phoenix). -------- = Comments = - blah, blah, blah....