473,804 Members | 3,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Graphs/statistics using wxPython

Hello all,

I wanted to plot some statistics, so I wrote a simple wxPython class
to do it. Then I realized that I would like to draw bar graphs, so I
added that too.

Since I'm a complete Python newbie, I haven't done much of it the
"Python way", I suspect. So, I'm wondering if someone would like to show
me some of the tricks I should have used.

You can find the code at:
http://user.it.uu.se/~jada3673/drawtest.py

It's not finished[*], but it is usable. Oh, you can do whatever you
want with it. Since I had never touched wxPython before working with
this, I used some doodle application as a starting point.

Btw... Has anyone already written statistics classes for wxPython? If
so, where can I find them? If nothing similar exists, I'll probably add
support for loading data from a file, and a few other features. Feel
free to make requests or enhancements.
[*] There are some size/offset bugs which I am aware of, but they are
easily fixed. I just haven't got around to it yet.
Jul 19 '05 #1
8 5125
Jan Danielsson wrote:
Hello all,

I wanted to plot some statistics, so I wrote a simple wxPython class
to do it. Then I realized that I would like to draw bar graphs, so I
added that too.

Since I'm a complete Python newbie, I haven't done much of it the
"Python way", I suspect. So, I'm wondering if someone would like to show
me some of the tricks I should have used.


Trick #1:

import matplotlib

;-)

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 19 '05 #2
Robert Kern wrote:
I wanted to plot some statistics, so I wrote a simple wxPython class
to do it. Then I realized that I would like to draw bar graphs, so I
added that too.

Since I'm a complete Python newbie, I haven't done much of it the
"Python way", I suspect. So, I'm wondering if someone would like to show
me some of the tricks I should have used.

Trick #1:

import matplotlib


Oh. :-)

That's a pretty neat trick -- now can you make my embarrassment go away?

I did do a quick search to see if anyone had done anything similar;
but I guess I wasn't using the right keywords.
;-)


You may point and laugh.

Damn, that's a neat toolkit! Thanks for pointing me to it!
Jul 19 '05 #3
Jan Danielsson wrote:
Robert Kern wrote:

Trick #1:

import matplotlib


Oh. :-)

That's a pretty neat trick -- now can you make my embarrassment go away?

I did do a quick search to see if anyone had done anything similar;
but I guess I wasn't using the right keywords.
;-)


You may point and laugh.

Damn, that's a neat toolkit! Thanks for pointing me to it!


It's okay. Just about every Pythonista in the sciences has, at one time
or another, started a plotting library. It's a rite of passage. Welcome
to the club. :-)

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 19 '05 #4
Robert Kern wrote:
[---]
It's okay. Just about every Pythonista in the sciences has, at one time
or another, started a plotting library. It's a rite of passage. Welcome
to the club. :-)


Question: I need to install SciPy in order to use matplotlib, but on
the download page I see that there are versions for Python 2.2.x and
2.3.x. I use 2.4.1 -- will bad things happen if I try to use the build
for 2.3.x?
Jul 19 '05 #5
Jan Danielsson wrote:
Robert Kern wrote:
[---]
It's okay. Just about every Pythonista in the sciences has, at one time
or another, started a plotting library. It's a rite of passage. Welcome
to the club. :-)
Question: I need to install SciPy in order to use matplotlib,


No you don't.
but on
the download page I see that there are versions for Python 2.2.x and
2.3.x. I use 2.4.1 -- will bad things happen if I try to use the build
for 2.3.x?


Yes. Scipy has many extension modules; such modules built for 2.3.x
won't work for 2.4.x, etc.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 19 '05 #6
Robert Kern wrote:
[---]
It's okay. Just about every Pythonista in the sciences has, at one time
or another, started a plotting library. It's a rite of passage. Welcome
to the club. :-)

Question: I need to install SciPy in order to use matplotlib,


No you don't.


Ah.. I misread. I read
http://matplotlib.sourceforge.net/installing.html, the section which
describes what "enthought python" contains, and thought it meant "this
is relevant to matplot lib".
but on
the download page I see that there are versions for Python 2.2.x and
2.3.x. I use 2.4.1 -- will bad things happen if I try to use the build
for 2.3.x?


Yes. Scipy has many extension modules; such modules built for 2.3.x
won't work for 2.4.x, etc.


So, for future reference: I should *never* mix x and y versions in
verion: x.y.z. I've wondered why there are versions of libraries for
different versions of Python..
Jul 19 '05 #7
Jan Danielsson wrote:
So, for future reference: I should *never* mix x and y versions in
verion: x.y.z. I've wondered why there are versions of libraries for
different versions of Python..


For packages with extension modules at least. Python maintains binary
compatibility between micro-releases (i.e. different z).

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 19 '05 #8
Jan Danielsson wrote:
Hello all,

I wanted to plot some statistics, so I wrote a simple wxPython class
to do it. Then I realized that I would like to draw bar graphs, so I
added that too.

Since I'm a complete Python newbie, I haven't done much of it the
"Python way", I suspect. So, I'm wondering if someone would like to show
me some of the tricks I should have used.

You can find the code at:
http://user.it.uu.se/~jada3673/drawtest.py

It's not finished[*], but it is usable. Oh, you can do whatever you
want with it. Since I had never touched wxPython before working with
this, I used some doodle application as a starting point.

Btw... Has anyone already written statistics classes for wxPython? If
so, where can I find them? If nothing similar exists, I'll probably add
support for loading data from a file, and a few other features. Feel
free to make requests or enhancements.

[*] There are some size/offset bugs which I am aware of, but they are
easily fixed. I just haven't got around to it yet.


If you ever want to add an extra dimension, then using OpenGL with
wxWindows is a breeze. See attached file for a 3d pie char..

Will McGugan

--
http://www.willmcgugan.com
"".join({'*':'@ ','^':'.'}.get( c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzptht na^pbz")

# Pie Chart Window
# 2005 Will McGugan (wi**@willmcgug an.com)
import wx

from wx import glcanvas
from OpenGL.GL import *
import math
import time

sin = math.sin
cos = math.cos
PI = math.pi
def DegToRad(deg):
return deg*PI/180.

class PieSegment(obje ct):

def __init__(self, radius, angle1, angle2, colour, depth):

self.display_li st = glGenLists(1)

glNewList(self. display_list, GL_COMPILE)

self.angle1 = angle1
self.angle2 = angle2
self.explode_an gle = DegToRad( (angle2 + angle1) / 2.)
DrawPieSegment( radius, angle1, angle2, colour, depth)

glEndList()

def __del__(self):
glDeleteLists(s elf.display_lis t, 1)

def Draw(self, angle1, angle2, explode):

glPushMatrix()

glRotate(angle1 , 1.0, 0.0, 0.0);
glRotate(angle2 , 0.0, 0.0, 1.0);

glTranslatef( sin(self.explod e_angle)*explod e, cos(self.explod e_angle)*explod e, 0. )

glCallList(self .display_list)
glPopMatrix()

def DrawPieSegment( radius, angle1, angle2, colour, depth):

angle1 = DegToRad(angle1 )
angle2 = DegToRad(angle2 )

# Number of divisions in 360 degrees
RES = 100.

fan2D = []

step_degree = ((2*PI)/RES)
step_count = int( (angle2 - angle1) / step_degree ) + 1

step_degree = ( angle2 - angle1 ) / float(step_coun t)
# Precalculate the trig
sina1 = sin(angle1)
cosa1 = cos(angle1)

sina2 = sin(angle2)
cosa2 = cos(angle2)

# Calculate the points in an arc
for p in xrange(step_cou nt+1):

a = angle1 + p * step_degree

x = sin(a)
y = cos(a)

fan2D.append( (x,y) )

z1 = +depth/2.
z2 = -depth/2.

glMaterial(GL_F RONT, GL_DIFFUSE, [colour[0], colour[1], colour[2], 1.0])

# Create a fan for the top and bottom of the pie segment
glNormal(0, 0, +1.)
glBegin(GL_TRIA NGLE_FAN)
glVertex(0, 0, z1)
for x, y in fan2D:
glVertex(x*radi us, y*radius, z1)
glEnd()

glNormal(0, 0, -1.)
glBegin(GL_TRIA NGLE_FAN)
glVertex(0, 0, z2)
for x, y in fan2D:
glVertex(x*radi us, y*radius, z2)
glEnd()

# A strip for the curved edge
glBegin(GL_TRIA NGLE_STRIP)
for x, y in fan2D:
glNormal(x, y, 0.)
xr = x * radius
yr = y * radius
glVertex(xr, yr, z1)
glVertex(xr, yr, z2)
glEnd()
n1 = -cosa1, sina1, 0.
n2 = cosa2, -sina2, 0.

x1, y1 = sina1 * radius, cosa1 * radius
x2, y2 = sina2 * radius, cosa2 * radius
# Two quads for the radius edges

glNormal(n1)
glBegin(GL_QUAD S)

glVertex(0, 0, z1)
glVertex(x1, y1, z1)
glVertex(x1, y1, z2)
glVertex(0, 0, z2)

glEnd()

glNormal(n2)
glBegin(GL_QUAD S)

glVertex(0, 0, z1)
glVertex(x2, y2, z1)
glVertex(x2, y2, z2)
glVertex(0, 0, z2)

glEnd()

class PieChartWindow( glcanvas.GLCanv as):
def __init__(self, parent):

attriblist = None
glcanvas.GLCanv as.__init__(sel f, parent, -1, wx.DefaultPosit ion, wx.DefaultSize, 0, "GLCanvas", attriblist, wx.NullPalette)

self.init = False

self.segments = []
self.animating = False
self.angle = 0.
self.explode = 0.

self.downx = 0
self.rot = 0.

self.captured_m ouse = False

self.timer = wx.Timer(self)

self.Bind(wx.EV T_TIMER, self.OnTimer)
self.Bind(wx.EV T_LEFT_DOWN, self.OnLeftButt onDown)
self.Bind(wx.EV T_LEFT_UP, self.OnLeftButt onUp)
self.Bind(wx.EV T_MOTION, self.OnMouseMov e)
self.Bind(wx.EV T_ERASE_BACKGRO UND, self.OnEraseBac kground)
self.Bind(wx.EV T_SIZE, self.OnSize)
self.Bind(wx.EV T_PAINT, self.OnPaint)

def Animate(self):
self.animating = True
self.start_anim ate = time.clock()
self.timer.Star t(1000/20)
def OnTimer(self, event):
if self.IsShown():
self.Refresh()
if not self.animating:
self.timer.Stop ();
def SetSegments(sel f, segments, angle=0., radius=150., depth=50.):

self.segments = []

First = True

if len(segments):
angle = -(segments[0][0]/2. *360./100.)

for seg in segments:

seg_percent, colour = seg

if seg_percent == 0:
continue

if seg_percent < .2:
angle = angle + (seg_percent*36 0./100.)
continue

if First:
angle = -(seg_percent/2. * 360./100.)
First = False

next_angle = angle + (seg_percent*36 0./100.)
self.segments.a ppend(PieSegmen t(radius, angle, next_angle, colour, depth))
angle=next_angl e

def PerspectiveProj ection(self):

w, h = 300., 300.
a = 30.

tana = math.tan(DegToR ad(a/2.))

glMatrixMode(GL _PROJECTION);

v = (w/2) / tana

near = v / 10.

vw = tana * near
vh = tana * near

glFrustum(-vw, vw, -vh, vh, near, v*3.0);

glMatrixMode(GL _MODELVIEW);
glTranslatef(0. 0, 0.0, -v*1.2);

glLight(GL_LIGH T0, GL_POSITION, [w/2, h/2, v*1.2, 0.0]);

def InitGL(self):

self.Perspectiv eProjection()

glMaterial(GL_F RONT, GL_AMBIENT, [0.3, 0.3, 0.3, 1.0])
glMaterial(GL_F RONT, GL_DIFFUSE, [0.9, 0.9, 0.9, 1.0])
glMaterial(GL_F RONT, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0])
glMaterial(GL_F RONT, GL_SHININESS, 70.0)
glLight(GL_LIGH T0, GL_AMBIENT, [0.25, 0.25, 0.25, 1.0])
glLight(GL_LIGH T0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
glLight(GL_LIGH T0, GL_SPECULAR, [.1, .1, .1, 1.0])

glLightModel(GL _LIGHT_MODEL_AM BIENT, [0.2, 0.2, 0.2, 1.0])
glEnable(GL_LIG HTING)
glEnable(GL_LIG HT0)
glDepthFunc(GL_ LESS)
glEnable(GL_DEP TH_TEST)

glClearColor(1, 1,1,1)
glClear(GL_COLO R_BUFFER_BIT | GL_DEPTH_BUFFER _BIT)
glMatrixMode(GL _MODELVIEW);
def OnLeftButtonDow n(self, event):
self.downx = event.GetPositi on()[0]
self.captured_m ouse = True
self.CaptureMou se()

def OnLeftButtonUp( self, event):
if self.captured_m ouse and self.HasCapture ():
self.ReleaseMou se()

def OnMouseMove(sel f, event):
if event.Dragging( ) and event.LeftIsDow n():
mx, my = event.GetPositi on()
rot_delta = self.downx - mx
self.rot+= rot_delta
self.downx = mx
self.Refresh()

def OnEraseBackgrou nd(self, event):
pass
def OnSize(self, event):
size = self.GetClientS ize()
if self.GetContext ():
self.SetCurrent ()
glViewport(0, 0, size.width, size.height)
self.Refresh()
event.Skip()

def OnPaint(self, event):
dc = wx.PaintDC(self )
self.SetCurrent ()
if not self.init:
self.InitGL()
self.init = True

self.OnDraw()
def OnDraw(self):

glClear(GL_COLO R_BUFFER_BIT | GL_DEPTH_BUFFER _BIT);

if self.animating:

t = time.clock() - self.start_anim ate
if t < 1.:
self.angle = 0.
self.explode = 0.
elif t < 2.:
ts = t - 1.
self.angle = -50. * ts
elif t < 3.:
ts = t - 2.
self.angle = -50.
self.explode = 15. * ts
else:
self.animating = False
self.angle = -50.
self.explode = 15.

for segment in self.segments:

segment.Draw(se lf.angle, self.rot, self.explode)

self.SwapBuffer s()

if __name__ == "__main__":

class TestFrame(wx.Fr ame):
def __init__(self):
super(TestFrame , self).__init__( None, -1, "Pie Chart Test", style=wx.DEFAUL T_FRAME_STYLE)
self.SetClientS ize(wx.Size(300 ,300))

pie_chart = PieChartWindow( self)

# List of tuples ( <Percentage of Pie>, ( <Red>, <Green>, <Blue> ) )
seg = [ ( 20, (1., 0.1, 0.1) ),
( 25, (0.05, .9, .1) ),
( 10, (.2, 0.1, 1.) ),
( 30, (.8, .9, .1) ) ]

pie_chart.SetSe gments( seg )
pie_chart.Anima te()

app = wx.App()
frame = TestFrame()
frame.Show(True )
app.MainLoop()
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3145
by: Mark Fenbers | last post by:
I am investigating Python for the sake of ultimately generating hydrographs (such as this: http://ahps.erh.noaa.gov/iln/ahps/RiverDat/gifs/prgo1.png) on-the-fly from a web server cluster. I have not used Python previously and do not yet know if Python is very useful for this or if I am wasting my time. Quite frankly, I am a little bewildered. Not only is there Python, but there are many extention modules which cloud up my view as to...
9
7075
by: rhmd | last post by:
I need to create image files (eg bmp or jpeg) of xy scatter graphs (i.e., graphs in which markers denote individual points; the markers need to be small polygons of various sizes, shapes, colors, shadings, etc. and there are thousands on them on each graph). Have been using MS Excel, but its limitations are unbelievable (only whole number sizes, no way around a 56 color palette, only low quality jpeg files so that when I publish the...
12
9503
by: | last post by:
I've trolled the lists, FAQs, and Net as a whole, but can't find anything definitive to answer this. We're looking for real-time graph capability (bar, line, etc), so that we can display telemetry from a robot system. There are a bunch of packages out there, but many seem to provide only static graphs (e.g. for scientific, financial data, etc). Does anyone currently display real-time telemetry using Python? Can anyone provide any...
3
2283
by: Kenneth McDonald | last post by:
If this is not an appropriate newsgroup for this type of posting, please let me know and (if possible) suggest an alternative. I've done a fair bit of research on the net, but information is scattered all over the place and I haven't been able to find mailing lists relating specifically to python and UIs. I'd like to start using Python for some GUI programming again. I'd like to use Tk, but it seems to be dying a slow death, so it's...
16
11884
by: David Lauberts | last post by:
Hi Wonder if someone has some words of wisdom. I have a access 2002 form that contains 2 graph objects that overlay each other and would like to export them as a JPEG to use in a presentation. I can do this individually for each graph but this does not help me as I need both on the same JPEG. I thought I would try an export the form that contains both but I am having trouble. (My VBA is self taught and a little knowledge is...
0
590
by: Ray Mitchell | last post by:
Hello, I need to write an application that displays multiple graphs on multiple tabbed sheets in a single window. The graphs are all simple X-Y line graphs like you'd see on an oscilloscope, but with labeled coordinates. I need everything (including the fonts if possible) to scale as I resize the window. I'm debating about using MFC or C# but I'd like to use C#. (I assume, however, that .NET will not work on Windows NT.) Is there...
17
5105
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also identifying memory leaks. The application in question is the Mozilla Web Browser. I also have had similar tasks before in the compiler construction area. And it is easy to come up with many more examples, where such kind of statistics can be very...
7
5575
by: Heiko Niedermeyer | last post by:
Sorry for the fuzzy subject... Currently I'm writing a little programm to extract some chemical information out of a text file, and then present it in a pleasant way. The Extraction works so far, so now the presentation will be next. As I'm learning Python from scratch, I don't care wether to use (=learn) TKinter or PyQt or whatever, I just need some advice, which suits my needs best. It would be nice to have the programm working...
1
2666
by: Antoninus Twink | last post by:
On 9 Nov 2008 at 9:34, marlow.andrew@googlemail.com wrote: Speaking for myself... I haven't downloaded your project because I've never heard of it. Generally, until a package makes it into my distribution (Debian), the odds of it being on my radar are very small. Of course, it's possible that I could come across the package while Googling. But in that case, your site on Sourceforge (http://depdot.sourceforge.net/) gives so little...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10580
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.