Archive for January, 2010

Interactive Contour -3d plot example program in matplotlib

Interactive contour  in matplotib , eg 1:

#begin of code
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import time

plt.ion()  #interactive on

fig = plt.figure()
ax = axes3d.Axes3D(fig)
X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contourf(X, Y, Z)
ax.clabel(cset, fontsize=9, inline=1)
plt.draw()

ans=''
while ans != 'q':

 ax = axes3d.Axes3D(fig)
 X, Y, Z = axes3d.get_test_data(0.05)
 cset = ax.contourf(X, Y, Z)
 ans=raw_input('enter level values or 'q' to quit : ')
 #for eg : cset.level=[-45,-30,-20,-10,0,10,15,30,45,60]

 cset = ax.contourf(X, Y, Z,cset.levels)
 ax.clabel(cset, fontsize=9, inline=1)
 plt.draw()

 time.sleep(3)
 plt.clf()

plt.ioff()    #interactive off
plt.close()

#End of code

The above python program will run the contour 3D view using matplotlib.

And we can give the input at run time to change the contour levels.

while running the above program , in prompt says like “enter level values or ‘q’ to quit:”

Enter the following
[-45,-30,-20,-10,0,10,15,30,45,60] and press Enter .

Now u can see the changes of contour levels in the same figure.

so u can give the various levles of contour. ( upto 10 levels ‘default’)

Enter ‘q’ to quit the interaction mode.

Interactive in matplotib , eg 2: Using file

save the following contour levels values in list manner and save it as “contour.txt”
contour.txt

[-45,-30,-20,-10,0,10,15,30]
[-55,-25,-15,2,8,40,50,78]
[-40,-15,-10,1,20,49,60]
[-35,-28,-15,0,10,20,40]

matplotlib.py


#begin of code
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import time

plt.ion()
f1 = open(r'contour.txt')
fig = plt.figure()
ax = axes3d.Axes3D(fig)
X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contourf(X, Y, Z)
ax.clabel(cset, fontsize=9, inline=1)
#ax.view_init(90, 0)
#(0,30) side view
#(90,0) top view

ans=''

plt.draw()
raw_input('enter')
plt.ion()
for ans in f1.readlines():

 ax = axes3d.Axes3D(fig)
 #ax.view_init(90, 0)
 X, Y, Z = axes3d.get_test_data(0.05)
 cset = ax.contourf(X, Y, Z)

 cset.levels=eval(ans.strip())
 print cset.levels
 cset = ax.contourf(X, Y, Z,cset.levels)
 ax.clabel(cset, fontsize=9, inline=1)
 plt.draw()

 time.sleep(3)
 raw_input('enter')
 plt.clf()
plt.close()
f1.close()

#end of code

Run the above python coding. It will take the input contour levels from the file “contour.txt” and the output shows like movie of 3d contour changes in its levels.

Outputs :

Initial levels in 3D contour : [ fill ]

code :

ax.contourf(X, Y, Z,cset.levels)

Changes in contour levels :

Contour in Line Drawing :

code :

ax.contour(X, Y, Z,cset.levels)


Changes in Line contour levels :

Contour in Top View :

code :

ax.view_init(90, 0)

Contour level changes in top view :

Contour in side view :

code :

ax.view_init(0, 30)

Contour changes in side view :

you can download theabove documentation in pdf format from here .,

Link : http://www.slideshare.net/arulalan/matplotlibinstallatininteractivecontourexampleguide

அருளாளன் . த

matplotlib Installation Guide

Hi

This guide will help to install matplotlib those who are all newbie to python and source installation.

Ref : http://matplotlib.sourceforge.net/users/installing.html

To install matplotlib , we need to 4 major depencies which are ,

1) python 2.4 (or later but not python3)
matplotlib requires python 2.4 or later

$ sudo apt-get install python

2) numpy 1.1 (or later)
array support for python

Download the source of numpy from here http://sourceforge.net/projects/numpy/files/NumPy/

untar the source using command $ tar zxvf filename.tar.gz     or  Right click the tarball  package and click on  the ” Extract here ” .

$ cd numpy-1.4.0  # i used to setup numpy

$ python setup.py build

$ sudo python setup.py install

3) libpng 1.1 (or later)
library for loading and saving PNG files.

Download the source of libpng from here http://www.libpng.org/pub/png/libpng.html

untar the source

$ cd libpng-1.2.41   # i used to setup libpng

$ ./configure

$ make

$ sudo make install

4) freetype 1.4 (or later)
library for reading true type font files.

Download the source of freetype from here http://www.freetype.org/

untar the source

$ cd freetype-2.3.9  # i used to setup libpng

$ ./configure

$ make

$ sudo make install

Now Download the source of matplotlib from

http://sourceforge.net/projects/matplotlib/files/

Install it by the following way .

untar the source

$ cd matplotlib-0.99.1.2   # i used to setup matplotlib

$ python setup.py build

$ sudo python setup.py install

Thats all. we installed matplotlib successfully . I installed all the above setup in ubuntu 9.10 ,  working perfectly !

அருளாளன் . த

contour program using contour module and cdat 5.2

Hi to all.  

Here i pasted example program to draw contour using 
"contour.py" module.

Download the corrected "contour.py" module from the previous
post of mine.

Link : https://tuxworld.wordpress.com/2010/01/15/setting-up-contour-module-in-cdat-5-2/

save the contour.py in /home/[user-name]/example/     location.

copy the below example program and save it as 
"sample.py" and pasted in that same folder.


import numpy as n
import numpy.ma as N
from contour import contour

x = N.arange(15) * 15.0 - 180.0
y = N.arange(15) * 15.0 - 360.0
data = N.outerproduct(n.sin(y*n.pi/360.), n.cos(x*n.pi/360.))

a=contour(data, x, y, title='Example')

a=raw_input('press enter to view levels in contour')
levs = [-0.4,-0.2,0,1.6,2.7,2.1,2.3,2.5]
#levs = [-0.8,-0.2,0,0.5,0.7,0.9]
contour(data, x, y, c_levels=levs)
b=raw_input('press enter to close ')

Run the program :

$ cd /usr/local/cdat5.2/bin

$ ./cdat /home/[user-name]/example/sample.py

Output :

[ In 2 Dimensional view ]

Setting up Contour Module in CDAT 5.2

Hi ,

During analysis about contour plotting tools for my project , i searched in Internet . I got few tools such as CDAT , Matplotlib and Mayavi.

First i tried to install CDAT in , ubuntu 9.10.

After successfully installed it , i very much tried to setup contour module with cdms in cdat.

But during upgrade of cdat from cdat 4.3 to cdat 5.2 , the cdat developers changed many supporting modules.

The upgraded changes are in

cdms to cdms2

numeric into numpy

and in Masked Array , etc.,

so i found the version conflict and i solved those problems.

Do the following steps to use contour.py module to draw contour using cdat 5.2.

Contour Module in CDAT 5.2

Download the contour.py module from the below link :

http://www.johnny-lin.com/py_pkgs/IaGraph/Doc/contour.html

we need to edit few lines because of cdms version upgraded in cdat 5.2.

comment the following lines at 41 from contour.py

import IaGraph_version

__version__ = IaGraph_version.version

del IaGraph_version

comment @193

#import MA

and add the following

import numpy.ma as MA

comment @ 195

#import Numeric as N

and add the following

import numpy as N

comment @ 196

#import cdms ,vcs

add the folowwing

import cdms2 as cdms

import vcs

Edit the following line @ 226

if MA.isMA(data) ==1:

if MA.isMA(data) ==0: # change from 1 to 0

Now we can run the contour module using cdms in cdat. !

or

download the modified “contour.py” from

http://pastebin.com/fad87374

i uploaded , the “contour.py” which is modified very slightly and its suitable for cdat 5.2 .

or

download the modified contour.py module in pdf format from here

http://www.slideshare.net/arulalan/contourpy-module

அருளாளன் . த

cdat 5.2 installation guide for ubuntu

Cdat 5.2 installation steps for ubuntu

Ref : http://www2-pcmdi.llnl.gov/

Ref : http://www2-pcmdi.llnl.gov/cdat

Ubuntu packages needed

If you have only the minimal Ubuntu install, there is a good chance that you won’t be able to do a successful install of CDAT, since additional packages are needed. Here’s the list of what I added (you can add them using Synaptic, Ubuntu’s GUI-based package manager):

  • bison *
  • byacc *
  • flex *
  • gawk *
  • g++
  • gfortran (for contrib packages)
  • libxt-dev
  • libx11-dev
  • tcsh (just highly recomended)
  • xorg-dev (also called xserver-xorg-dev)

If  you will install cdat in freshly installed ubuntu , then finally it will return “Error” like , can not install “matplotlib”.

So for that , we need to install

libpng (1.1 or later ) and freetype (1.4 or later)

from synaptic package manager.

Explicitly set the default compiler

If you don’t explicitly set the environment variables specifying which compilers to use, you may end up compiling different libraries with different compilers. Thus, in csh or tcsh, make sure you execute:

export CC=gcc
export FC=gfortran
export CXX=g++

source :

svn export http://www-pcmdi.llnl.gov/svn/repository/cdat/tags/5.2    cdat-5.2-src

Once you got the sources you can install CDAT as follow:

./configure --prefix=/usr/local/cdat5.2/ --with-externals=/usr/local/cdat5.2/cdat_ext

NOTE : To copy the above code highlight the mouse over the code and select the “view source” option.A popup window will display the code.you can copy the code


$ sudo make   # You can now start CDAT with the command 

$ cd /usr/local/cdat5.2/bin 

$ ./cdat

About : Thyagarajan Shanmugham

Hi to all,

I am very luckiest guy to get such a wonderful and friendly guide .                                                                         

yes. He is Mr.Thyagarajan Shanmugham.

From past one year , we know about each other.  I know him by my brother shrinivasan only.

He gave me intro to Thyagu.

He is very knowlegable person which i never seen before.

Now I am going to his home everyday and doing my project .

Everyday we will discuss about project and whenever i struggling he will help to over come out from those problems.

He always encouraging me.

Hats off to him.

I can do anything behind of him.

அருளாளன் . த

My Project

Hi ,

I am doing final year MCA in saveetha engg college. Right now i m in 6 th semester i.e. project semester.

we are doing post – graduate know, so we have to do an individual project only.

I am doing project on ” Contour analysis and visualization ” using Matplotlib , CDAT, Mayavi , etc.,

My Guide is Mr.Thyagarajan  , active member of ilugc .

This is my project documentation .

Download Link :

http://www.slideshare.net/arulalan/contour-analysis-and-visulaization-documetation-1

அருளாளன் . த