Archive for May, 2009

 

Making Octave ploting nicer – 21. May, 2009

Personally I prefer python over Octave/Matlab. But since I’m being forced to write Matlab code for one of my course, I started playing around with octave.
By default octave makes ugly ugly plots because it uses the X11 terminal. But there’s a solution, you can switch to wxt terminal. In order to do that, add in the bottom of your .bashrc the following line:
export GNUTERM=wxt

And a magic will happen:
Before:

UGLY !!!

UGLY !!!

After:

NICE !!!

NICE !!!

That’s all folks.

Posted in Debian, Linux, science

3D plots with matplotlib – 17. May, 2009

Today I built matplotlib from svn. The answer why I did it is in the post’s title. 3D plots are back to this wonderful tool !

So, if you build matplotlib version 0.98.6svn or later, you can enjoy this. It’s sometimes a little bit cranky. With plots of multiple points I got one big black surface, but for simple stuff it works great.

Here is an example of 3D plot of hydraulic head on a coordinate system:

import pylab as pl
from numpy import *
import mpl_toolkits.mplot3d.axes3d as axes3d

       #  x      y     head
head = ((25, 225 , 240.1178), #h1
		(75,  225,  242.3238),#h2
		(125,   225, 244.8013),#h3
		(175,   225,    247.2736),
		(225,   225,    248.8057),#h5
		(25, 175 ,  241.7646), #h6
		(75,  175,  242.0468),#h7
		(175,   175, 248.2085),  #h8
		(225,   175,    249.1382),#h9
		(25,   125,    243.1239), #h10
		(225, 125 , 249.5332), #h11
		(25,  75,  244.4780),#h12
		(75,   75, 245.1523),  #h13
		(175,   75,    248.9717),#h14
		(225,   75,    249.4562),#h15
		(25, 25 ,  245.1523), #h16
		(75,  25,  245.8214),#h17
		(125,   25, 247.1543),  #h18
		(175,   25,    248.4819),#h19
		(225,   25,    249.3144)) #h20       

x, y, z = zip(*head)
xi, yi = pl.arange(0, 250, 5), pl.arange(0, 250, 5) #create grid
head = pl.griddata(x, y, z, xi, yi) #interpolate the scattered data !
print shape(head)
print shape(xi)
print shape(yi)
f = pl.figure(1)
pl.scatter(x, y)
pl.contour(xi, yi, head)
pl.colorbar() # draw colorbar
#pl.show()

f = pl.figure(2)
ax = axes3d.Axes3D(f)
#X,Y,Z = axes3d.get_test_data(0.05)
cset = ax.contourf3D(xi,yi,head)
ax.clabel(cset, fontsize=9, inline=1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Hydraulic Head')

f = pl.figure(3)
ax = axes3d.Axes3D(f)
#X,Y,Z = axes3d.get_test_data(0.05)
cset = ax.contour3D(xi,yi,head)
ax.clabel(cset, fontsize=9, inline=1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Hydraulic Head')

pl.show()

and the output is:
head 3D

Another 3D plot

Another 3D plot


old fashioned...

old fashioned...

As usual, have fun with python !

Posted in Python, science