Plotting 2D Unstructured Data Using Free Software

As part of my work I need to do a lot of plots of 2D unstructured data. That is data points that we need to plot contours and vectors of data that is not necessarily in a regularly spaced grid. The original program that we use GSharp is proprietary and I was looking for a free software alternative.

There are generally two steps involved in plotting unstructured data sets – interpolating the data onto a structured grid and then getting the plotting system to umm plot. This page is a semi-random selection of free software scripts for plotting things out and the output therein.

The data is available in three column format in a file called contourdata.dat which is available on this webpage should you wish to see if you can get a better result than me!


Firstly the program we are trying to replace the proprietary GSharp. This program has quite a neat GUI interface that actually produces scripts in a scripting language rather than a binary output file – this makes it quite easy to generate scripts interactively without frequent reference to the manual. But the point of this page is to look at the quality of the output which looks like (click on the picture of a bigger one).

Contours of Total Pressure Loss

The data is from a three dimensional computational fluid dynamics simulation of flow through a low speed linear cascade the details of which are unimportant but you can find more about in my thesis.


Gnuplot is a venerable plotting program that has recently had a major update to version 4.2. The following script was used to plot the data:
set terminal png nocrop enhanced font arial 8 size 800,600
set output "gnuplot_contours2.png"
set dgrid3d 40,40,16
set cntrparam levels incremental 0,0.2,2.0
set contour base
unset surface
set view 0,0
set xlabel "tang / [m]"
set ylabel "radial / [m]"
set title "Loss Coefficient CFD Jan 2006 - Gnuplot"
splot "contourdata.dat" with lines

The results are thus:

Gnuplot contours

Now this may not look that impressive but the dgrid3d function used is only meant to be a rough interpolation routine and I’m sure a gnuplot guru could do a better job. And gnuplot is quick.


gri is a language for Scientific plotting, after a bit of work I got the following script to accomplish what I wanted:

# Contouring ungridded data using gri
# Some plot variables
.min. = 0
.max. = 1.6
.inc. = 0.1
.inc2. = 0.2
# read data
open contourdata_gri.dat
read columns x y z

# axes
set x axis -0.25 -0.03 0.05
set x name “tang / [m]”
set y axis 0 0.2 0.05
set y name “radial / [m]”
draw axes

# set grid sizes – note that the size of grid has
# a big influence on how good the contours are
set x grid -0.22 -0.04 0.001
set y grid 0 0.2 0.001
set contour labels rotated
#set contour labels nowhiteunder

# Use default method (Barnes)
convert columns to grid

# To get filled contour plots in gri:
# convert the grid to an image and then plot the image
set image range .min. .max.
convert grid to image size 1000 1000

# Colour Scale
set image colorscale hsb 0.666 1.0 1.0 .min. hsb 0.0 1.0 1.0 .max.
draw image
set font size 8
draw contour .min. .max. .inc2. .inc.
set font size 12
draw title “Loss Coefficient CFD Jan 2006 – gri”

Then I used the convert program to turn the postscript output to a png:

Contour plot produced by the gri plotting program


Currently for most of my work I use matplotlib.  Although strictly speaking this doesn’t do the interpolation of data you have to get a Scientific Library to do this.  The library is called SciPy and matplotlib is a dependency of said library.  One furhter complication is that to use the script given below one needs to enable a sandbox module, i.e. you need to install SciPy from source and edit the file: scipy-0.6.0/scipy/sandbox/ to uncomment the delauny subpackage.  Once that is done the following script usually works:

from pylab import *
from scipy import *
from scipy.sandbox.delaunay import *

fin = open(“contourdata.dat”,”r”)
data = io.read_array(fin,comment=’#’)

x = data[:,0]; y = data[:,1]; z = data[:,2];
xvalues = arange(-0.2288,-0.0387,0.001)
yvalues = arange(0,0.2,0.001)
xi, yi = meshgrid(xvalues,yvalues)

# triangulate data
tri = Triangulation(x,y)
# interpolate data
interp = tri.nn_interpolator(z)
zi = interp(xi,yi)

# do the acutal plotting
levels = arange(0,1.7,0.1)
CSF = contourf(xi,yi,zi,levels)
CS = contour(xi,yi,zi,levels,colors = (‘k’))
title(‘Loss Coefficient CFD Jan 2006 – mpl’)
xlabel(‘tang / [m]’)
ylabel(‘radial / [m]’)

Contour plot from matplotlib

Contour plot from matplotlib


16 thoughts on “Plotting 2D Unstructured Data Using Free Software

  1. Slasher

    Nice example of matplotlib. I love Python myself and right now I am trying to make a contourf plot on a FEM mesh (I am not succeding…). I thought this example was a way to do it but that sandbox.delaunay thing in the scipy package got to advanced for me. Well, thanks anyway, maybe I get some other idees from your script.

    1. grantingram Post author

      I think this is all a bit out of date now, and some things are a bit easier.

      The sandbox thing in scipy won’t work any more as I think they have removed the sandbox feature. I tend to use Octave for most of my work now and some time real soon I will get around to updating this page!

  2. Darkside

    I found your post really useful.
    You’re right: sandbox is not longer supported, but most of its functions are being translated to some other modules.
    Here you have my own version of your script, a little bit up-to-date:

    # -*- encoding: utf8 -*-

    import sys
    from scipy import *
    from matplotlib.delaunay import *
    import pylab as p

    def readfile(file):
    x, y ,z = loadtxt(file,delimiter=’,’,unpack= True)
    return x, y, z

    def defgrid(x,y):
    paso = 10.
    intervalx = (x.max()-x.min())/paso
    intervaly = (y.max()-y.min())/paso
    xvalues = arange(x.min(),x.max(),intervalx)
    yvalues = arange(y.min(), y.max(),intervaly)
    xi, yi = meshgrid(xvalues,yvalues)
    return xi,yi

    def interpolacion(x,y,z,xi,yi):
    # triangulate data
    tri = Triangulation(x,y)
    # interpolate data
    interp = tri.nn_interpolator(z)
    zi = interp(xi,yi)
    return zi

    # do the acutal plotting

    def plotear(xi,yi,zi):
    levels = arange(zi.min(),zi.max(),(zi.max()-zi.min())/10)
    CSF = p.contourf(xi,yi,zi,levels)
    CS = p.contour(xi,yi,zi,levels,colors = (‘k’))
    # add a vertical bar with the color values
    cbar = p.colorbar(CSF)‘I+II+III’,fontsize=12)

    def main():
    filename = sys.argv[1]
    x, y, z = readfile(filename)
    xi, yi = defgrid(x,y)
    zi = interpolacion(x,y,z,xi,yi)

    if __name__ == “__main__”:
    if len (sys.argv) == 2:
    main ()
    print “How to use: %s data_file ” % sys.argv[0]

    I hope you find it useful.

    1. Joel Rodriguez

      Good job, your code work’s great, no need for
      from scipy import *
      it could be commented out which greatly eases the use,
      as installing atlas, lapack and blas is not required.

      from numpy import *

      grantingram, Darkside, thanks.

      muy bien,

  3. Summer Camps

    Best you could make changes to the webpage name title Plotting 2D Unstructured Data Using Free Software Software Configuration Tips from an Idiot to something more catching for your blog post you make. I enjoyed the blog post even sononetheless.

  4. Pingback: Plotting 2D Unstructured Data Using Free Software – an update. | Software Configuration Tips from an Idiot

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s