This is an archival dump of old wiki content --- see for current material


E-mail: <wilnadutoit AT SPAMFREE gmail DOT com>

Simple 3D plotting in Python


   1 >>> import OpenGL.Tk

causes a segmentation fault on *ubuntu Fiesty (32 and 64-bit). Easy fix here (scroll down to the last post):

For Mat3D to work, you need to install OpenGL and Tk bindings for Python. For example, if you're using Debian or a 'derivative' thereof (like Ubuntu, Kubuntu or Xubuntu) then you should install the following packages:

sudo apt-get install python-opengl python-tk

This will (should) rope in some other required packages (tcl8.4 and blt) automatically.


If you get an error complaining about a missing package or command named togl when using Mat3D, you might probably need to download and install togl to your tcl lib folder. On Ubuntu 9.04, the tcl lib folder might be /usr/share/tcltk/tcl8.4 or /usr/share/tcltk/tcl8.5. Follow the instructions in the togl readme file - only a few files need to be copied.

If you want to save your plots, then you also need Python's imaging library (PIL):

sudo apt-get install python-imaging

Mat3D provides a few routines for basic 3D plotting. It is still very basic and there is at least one nagging bug; anti-aliasing sometimes produces weird effects on our Linux machines, but not under Windows. (Our graphics drivers are suspected to cause this, we'll look into it.)

You can download Mat3D from here:, examples included in or mat3d.tar.gz

   1 """Simple example for showing surface mesh functionality
   2    Variables that can be set in mat3d.mesh :
   3    colors = Colors   - Sets the colormap
   4    linewidth = 0.1   - Sets the width of the plotlines
   5    precision=3       - Sets the number of digits accuracy for the text on the axis    
   6    num_ticks=5       - Number of ticks on axes, evenly spaced 
   7    fill_mesh = 0     - If one, fills the mesh with the colormap in colors"""
   9 import numpy as N
  10 import mat3d as M
  11 from matplotlib import numerix as nu
  13 u = N.linspace(0, 2*N.pi,20)
  14 v = N.linspace(0, N.pi,20)
  16 x = nu.outerproduct(N.cos(u),N.sin(v))
  17 y = nu.outerproduct(N.sin(u),N.sin(v))
  18 z = nu.outerproduct(N.ones(N.size(u)), N.cos(v))
  20 # call the actual plotting routine
  21 x = M.mesh(x,y,z,fill_mesh=1,precision=1,num_ticks = 3)

Once you have the plot on screen, you can rotate, pan and zoom by pressing one of the mouse buttons (depending on what you want to do) and moving the mouse -- this is probably obvious...

Below is the screenshot of the above script. It's actually a bunch of images sewn together with 'gifsicle' to create an animated GIF -- which is an easy way to do it.


Below is another screenshot, this time a PNG from ''.


Example name



line plotting


multiple line plotting


surface plotting


surface plotting (different colormap)


plots sphere


plots points



SciPy: WilnaDuToit (last edited 2015-10-24 17:48:23 by anonymous)