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).
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
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 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
read columns x y z
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]”
# 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.
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:
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=’#’)
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]’)