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!

## GSharp

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).

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

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:

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

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

close

# 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:

## Matplotlib

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/setup.py`

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=’#’)

fin.close()

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’))

clabel(CS)

title(‘Loss Coefficient CFD Jan 2006 – mpl’)

xlabel(‘tang / [m]’)

ylabel(‘radial / [m]’)

show()

AlexMYour blog is interesting!

Keep up the good work!

NolanEZpyu2OSTLNDI

SlasherNice 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.

grantingramPost authorI 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!

DarksideI 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:

_______________________

#!/usr/bin/python

# -*- 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):

p.figure(figsize=(10,6))

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’))

#p.clabel(CS)

p.title(‘alfalfa=0.5’)

p.xlabel(‘p’,fontsize=12)

p.ylabel(‘rs’,fontsize=12)

# add a vertical bar with the color values

cbar = p.colorbar(CSF)

cbar.ax.set_ylabel(‘I+II+III’,fontsize=12)

cbar.add_lines(CS)

p.show()

def main():

filename = sys.argv[1]

x, y, z = readfile(filename)

xi, yi = defgrid(x,y)

zi = interpolacion(x,y,z,xi,yi)

plotear(xi,yi,zi)

if __name__ == “__main__”:

if len (sys.argv) == 2:

main ()

else:

print “How to use: %s data_file ” % sys.argv[0]

__________________________

I hope you find it useful.

DarksideI’m sorry. It doesn’t copy the tab spaces into the python program. I don’t know why.

grantingramPost authorThanks for that! It looks neat – one day I will find the time to update the page!

Joel RodriguezGood 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.

but:

from numpy import *

grantingram, Darkside, thanks.

muy bien,

🙂

Joel

HEIDI FARLEYI just like the style you took with this subject. It isn’t often that you just discover something so to the point and informative.

grantingramPost authorI’m glad you find it useful!

mutuellesThank’s for this nice tips

mutuelle santé

Meinhard JakobsThere is obviously a lot to know about this. I think you made some good points in Features also.

Keep working ,great job!

wisconsin union theater west side storyAnother a very great post by the author looking forward to visit more very soon.

Summer CampsBest 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.

Abhijeet OundhakarThanks Grant, and thanks to DarkSide too. Works beautifully.

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