Using wxPython for Plotting

vegaseat 1 Tallied Votes 2K Views Share

The wxPython GUI toolkit makes a very nice plotting component/widget available. You can select line or marker plotting, or a combination of both. All you have to supply is a list of x,y point tuples. Other features include zooming and printing.

# using wxPython for plotting
# tested with Python24, wxPython26 and wxPython28   vegaseat   19apr2007

import wx
import wx.lib.plot as plot

class MyFrame(wx.Frame):
    def __init__(self):
        self.frame1 = wx.Frame(None, title="wx.lib.plot", id=-1, size=(410, 340))
        self.panel1 = wx.Panel(self.frame1)
        self.panel1.SetBackgroundColour("yellow")
        
        # mild difference between wxPython26 and wxPython28
        if wx.VERSION[1] < 7:
            plotter = plot.PlotCanvas(self.panel1, size=(400, 300))
        else:    
            plotter = plot.PlotCanvas(self.panel1)
            plotter.SetInitialSize(size=(400, 300))
        # enable the zoom feature (drag a box around area of interest)
        plotter.SetEnableZoom(True)
        
        # 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')
        plotter.Draw(gc, xAxis=(0,15), yAxis=(0,15))
        
        self.frame1.Show(True)


app = wx.PySimpleApp()
f = MyFrame()
app.MainLoop()
aernie 0 Newbie Poster

Wonder why this returns an error:
python wxplotexample.py
Traceback (most recent call last):
File "wxplotexample.py", line 37, in <module>
f = MyFrame()
File "wxplotexample.py", line 15, in __init__
plotter = plot.PlotCanvas(self.panel1, size=(400, 300))
TypeError: __init__() got an unexpected keyword argument 'size'

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

Does not use full window for plotting, otherwice OK for me.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Does not use full window for plotting, otherwice OK for me.

You must have an older version of wxPython. The latest wxPython version 2.8.11.0 for Python26 works just fine.

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

print wx.VERSION gives
(2, 8, 10, 1, ''), so not so old one. OK, I update that.

Don't know the user aernie's trouble though...

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

OK, I removed all my Python 2.6, because for some reason the windows size had no effect whatsoever, even after taking out PythonPath and removing Python entries from Path.

Installed Python 2.7rc1, wxPython for that, Python 2.6.5, wxPython for that, numpy, psyco...

I run finally the wx and got the error of the original poster, probably connected with multiple installation of Python. As I had no patience to read up the document for multiple installation I ran the wx installer again (with the select this as default option enabled) and running the program from 2.6 (2.7 has not numpy available, so no plot). After that the IDLE for version 2.7 stopped running though.. But I fixed a garbage entry in Path variable and it started to work again.

UPDATE: No the problem don't go away: resize the window before closing it and it will not start at original size next time and the size parameter has no effect. But the error did go away.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

print wx.VERSION gives
(2, 8, 10, 1, ''), so not so old one. OK, I update that.

Don't know the user aernie's trouble though...

That version of wxPython should work, it works fine on my XP notebook with Python 2.6.5.
Not sure why user aernie has a problem with the old wxPython26 line. That line shouldn't even come up if the current wxPython28 is used.

Originally wxPython had a problem with Python version 2.6, but that was a long time ago.

I wouldn't fool around with Python 2.7, it's still in testing.

aernie 0 Newbie Poster

I removed all traces of earlier version of wx or wxPython until only version 2.8+ of wx remains in my Ubuntu 10.04 box. It also removed SPE, Kiki and other installed programs dependent on the 2.6.+ version of wx.

When I rerun the program it displayed the expected figures. Actually I was a bit embarassed to notice that the first post here was in 2007!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.