Data Plotting in wxPython

Using wx.lib.plot

wxPython has its own plotting library, which provides simple way of drawing large number of data on a canvas. It is convenient to use and it is fast. However you have only one axis per canvas and you can plot 2D graphs only.

To draw a line graph like above, create line objects using numpy

  214                 x = np.linspace(0,10,500)
  215                 y = np.sin(x)
  216 
  217                 # create lines
  218                 line1 = wxplot.PolyLine(list(zip(x, np.sin(x))),
  219                         colour='red', width=3, style=wx.PENSTYLE_DOT_DASH)
  220                 line2 = wxplot.PolyLine(list(zip(x, -np.sin(x))),
  221                         colour='blue', width=3, style=wx.PENSTYLE_LONG_DASH)

Then generate a graphics object and render it on the canvas. Here the canvas is implemented on a wxPanel. So you can embed it into any wx.Window object.

    1                # create a graphics
    2                 graphics = wxplot.PlotGraphics([line1, line2])
    3                 self.pnlPlot.Draw(graphics)

Using Matplotlib WXAgg backend

For more professional plot, you can use matplotlib, more specifically matplotlib WXAgg backend, where almost all the matplotlib features are available to wx.Python.  Thus you can create plots like below very easily.

The WXAgg Figure object and the FigureCanvas object are implemented on a wx.Panel as class members.

   40         # mpl figure object
   41         self.figure = Figure()
   42         # mpl canvas object
   43         self.canvas = FigureCanvas(self, -1, self.figure)

The shade plot on the left for example was generated by the code below.

  138                 # clear previous plot
  139                 self.pnlPlot.Clear()
  140                 # acquire new axes
  141                 ax1 = self.pnlPlot.AddSubPlot(121)
  142                 # we need figure object too
  143                 fig = self.pnlPlot.GetFigure()
  144 
  145                 # colormap
  146                 cmap = matplotlib.cm.copper
  147 
  148                 # import LightSource
  149                 from matplotlib.colors import LightSource
  150 
  151                 y,x = np.mgrid[-4:2:200j, -4:2:200j]
  152                 z = 10 * np.cos(x**2 + y**2)
  153                 ls = LightSource(315, 45)
  154 
  155                 rgb = ls.shade(z, cmap)
  156 
  157                 ax1.imshow(rgb, interpolation='bilinear')
  158                 im = ax1.imshow(z, cmap=cmap)
  159                 #im.remove()
  160                 #fig.colorbar(im)
  161                 ax1.set_title('shaded plot')

(source code)