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

அருளாளன் . த

Advertisements

5 responses to this post.

  1. Pictures are too small 😦

    Reply

  2. Posted by Anjana Talapatra on July 15, 2011 at 10:05 pm

    Hi I was going through your post and it was very helpful. I was wondering if you could help me with something. I have a contour plot which I then extended to 3D. What I now have is a step-like surface which looks continuous if viewed from the top but there are gaps if viewed from the sides because values increase instantly. Is there a way to fill it in so its actually looks like a stepped surface?
    Thanks!

    Reply

  3. Hai,

    If you will use Mayavi, then it may solve your problem.

    http://code.enthought.com/projects/mayavi/

    But for that you may need to study many things in that mayavi, examples, tutorials …

    Wishes…

    Reply

  4. http://helkj.narod2.ru AllSubmitter – Список для раскрутки сайта. Максимально качественная, быстрая и контролируемая регистрации в любых информационных ресурсах Интернета.

    Reply

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

%d bloggers like this: