Tuesday, November 22, 2011

Draw 2-D contour figure using Python

File should be in ASCII format read in 2 dimensions (an example file can be downloaded). File to read it is called "baca_2d .py". The Python Script file is presented as follows,

—————————————————— baca_2d.py ————————————


import numpy as np
import matplotlib.pyplot as plt
from numpy import *
b = []
data = open("scen-7.pma","r")
barp = data.readline()
bar  = barp.split()
n    = bar[0]
m    = bar[1]
baris = data.readlines()
for i in range(int(m)):
 kolom = baris[i].split()
 a = list(kolom)
 b = b + a
c = list(b)
d = reshape(array(c),(float(m),float(n)))
cs = plt.contour(d,100)
plt.clabel(cs, fontsize=9, inline=1)
cs = plt.show()

——————————————————- baca_2d.py ————————————




When the file is over run with the command,


abuadnan@abuadnan-laptop:~/Documents$ ./baca_2d.py


Then will come by the view as follows,




Figure 1. Plotting contur if python script in running.

From Figure 1, to take the contur generated, icon "save" can be click  so that title would look as follows,



Figure 2. Save the picture in the format of the image (e.g., *. png).


Enter the name of the file and the desired image format (in this example is used *. png) then press "save". The image file will be generated, such as the following example,

Figure 3. An example contour image is generated by the python script.


Figure 3 is a picture generated from a python script file with the name "baca_2d. py" run like the procedure above. Where this script reads the data file in ASCII format with the name "scen-7.pma". The File is read and resulting the Figure 1. From the Figure 1 the image file can be saved with the name, and the contour format as desired.


So, hopefully this is helpful to all users of linux or windows that use Python as a free program (free softwares). This example File can be downloaded from here (101,2 Kb).










No comments:

Post a Comment