Plotting 2D Unstructured Data Using Free Software – an update.

A while ago (ten years ago!) I wrote a post called  Plotting 2D Unstructured Data Using Free Software

I thought it might be useful to revisit that post to see how things have moved on.  Nowadays I do most of my plotting in Matplotlib – a library which has matured nicely since 2007, though I get confused quite a lot 😦

In the previous post I looked at GSharp, gri, gnuplot and matplotlib.  I will talk here about only about gnuplot and matplotlib.  Amazingly gnuplot has changed very little – the script I wrote ten years ago works without any modification and produces the same sort of output, i.e. not hugely impressive.  You might be able to get better outputs if you have more skill with gnuplot than me.

gnuplot_contours2

The matplot script however required modification to run.  A user called darksid3 pointed out a much better way of doing in 2010 but I think the matplotlib library has developed somewhat so the code is now very straightforward.  My script is now as follows:

#!/usr/bin/python
"""
Script to plot contour data using Matplotlib
"""
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import griddata

# Read and assign data
data = np.genfromtxt('contourdata.dat',names=['x','y','cp0'])
x = data['x']
y = data['y']
z = data['cp0']

# Do the interpolation
xi = np.linspace(x.min(), x.max(), 500)
yi = np.linspace(y.min(), y.max(), 500)
zi = griddata((x, y), z, (xi[None,:], yi[:,None]), method='cubic')

# Plot using the interpolated data
plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)
plt.colorbar(label="cp0")
plt.xlabel("tang / [m]")
plt.ylabel("radial / [m]")
plt.title("Loss Coefficient CFD Jan 2006 - matplotlib")

plt.show()

You might be able to make it a bit smaller but I favour a longer, readable code over something that is very compact.  The output is pretty straightforward too:

matplotlib_gridata_contours

A better reference can be found in the SciPy cookbook.

So it is nice to see things advancing in the free software world!!

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s