2D plot of filled contour with Plplot libraries using plshades

In the previous post, we show how to plot 2D data with plcont. It plots the line contour. Now, how about if we want something more colorful? we can use a filled contour plot with the command plshade. The syntax is similar. But first, let’s look at the complete program.

program simple
use plplot
implicit none
real(plflt),dimension(6) :: x,y
real(plflt),dimension(6,6)::z,x2d,y2d,z2
real(plflt)::xmin,xmax,ymin,ymax,zmin,zmax
real(plflt),dimension(10)::clevel
real::step
integer::nlevel
integer::i,j
character*1 defined
real(plflt),dimension(11)::shedge,shedge2
integer::fill_width
integer::cont_color
integer::cont_width
x=(/1,2,3,4,5,6/)
y=x
do i=1,6
  do j=1,6
    z(i,j)=x(i)*2*y(j)
    z2(i,j)=x(i)**2*y(j)
    x2d(i,j)=x(i)
    y2d(i,j)=y(j)
  enddo
enddo
call plinit()
xmin=1.0
xmax=6.0
ymin=1.0
ymax=6.0
nlevel=10
fill_width=2
cont_color=0
cont_width=0
zmin=minval(z)
zmax=maxval(z)

do i=1,nlevel+1
   shedge(i)=zmin+(zmax-zmin)*dble(i-1)/dble(nlevel)
   shedge2(i)=minval(z2)+(maxval(z2)-minval(z2))*dble(i-1)/dble(nlevel)
enddo

call plenv(xmin,xmax,ymin,ymax,0,0)
call pllab('X','Y','Test 2D plot')
call plshades(z,defined,1._plflt,6._plflt,1._plflt,6._plflt,&
     shedge,fill_width,cont_color,cont_width,x2d,y2d)
call sleep(2)
call plshades(z2,defined,1._plflt,6._plflt,1._plflt,6._plflt,&
     shedge2,fill_width,cont_color,cont_width,x2d,y2d)
call plend()

end program simple

In this program we plot 2 functions “z” and “z2”. the main command is:

call plshades(z,defined,1._plflt,6._plflt,1._plflt,6._plflt,&
shedge,fill_width,cont_color,cont_width,x2d,y2d)

The examples describes the parameter this way (note that it is different with the manual, sigh):

plshades(a, defined, xmin, xmax, ymin, ymax, clevel, fill_width, cont_color, cont_width,x2d,y2d)

where “a” is the 2D array of the data. The “defined” accepts x and y coordinate where we want to exclude the data, it must gives 0 for excluded region and 1 otherwise. The xmin, xmax, ymin, and ymax has the usual meaning which gives the range of the data to be plotted. The “clevel” is the a kind of contour level in plcont, but in our case it is the boundary for the filled contour regions. So for 10 level, we have to define 11 “clevel”. The values for this “color” level is computed from:

shedge(i)=zmin+(zmax-zmin)*dble(i-1)/dble(nlevel)

when i=1, shedge(1)=zmin. then for i=2, shedge(2)=zmin+width, where width=(zmax-zmin)/nlevel is the width between one contour level to another contour level.

Now, to be honest, I am not sure about what “fill_width” , “cont_color”, and “cont_width” does. I tried to change the values but can’t see any changes. All I know is that they take integer value as its input. But let me show you what the manual says, maybe you can understand it.

fill_width (PLINT, input): Defines width used by the fill pattern.
cont_color (PLINT, input): Defines pen color used for contours defining edges of shaded regions. The pen color is only temporary set for the contour drawing. Set this value to zero or less if no shade edge contours are wanted.
cont_width (PLINT, input): Defines pen width used for contours defining edges of shaded regions. This value may not be honored by all drivers. The pen width is only temporary set for the contour drawing. Set this value to zero or less if no shade edge contours are wanted.

The last two parameter is obvious, they are the 2D array for the x and y coordinates.

The plot can be seen in the image below:
shade2d

2D Contour plot using plplot function “plcont”

In the previous post, we describe how to do a simple line plot. In this post, I will describe how to plot contour using Plplot libraries, particularly using the “plcont” function. Let’s look at the code first.

program simple
use plplot
implicit none
real(plflt),dimension(6) :: x,y
real(plflt),dimension(6,6)::z,x2d,y2d
real(plflt)::xmin,xmax,ymin,ymax,zmin,zmax
real(plflt),dimension(10)::clevel
real::step
integer::nlevel
integer::i,j
x=(/1,2,3,4,5,6/)
y=x
do i=1,6
  do j=1,6
    z(i,j)=x(i)*2*y(j)
    x2d(i,j)=x(i)
    y2d(i,j)=y(j)
  enddo
enddo
call plinit()
xmin=1.0
xmax=6.0
ymin=1.0
ymax=6.0
nlevel=10
zmin=minval(z)
zmax=maxval(z)
step=(zmax-zmin+1)/nlevel
do i=1,nlevel
  clevel(i)=zmin+i*step
enddo
call plenv(xmin,xmax,ymin,ymax,0,0)
call pllab('X','Y','Test 2D plot')
call plcont(z,1,6,1,6,clevel,x2d,y2d)
call plend()

end program simple

As usual, we need to declare the variables needed in the beginning part of the code. The important part starts with

call plinit()

which is used to initialize the Plplot library.


do i=1,nlevel
clevel(i)=zmin+i*step
enddo

This code is used to compute the contour level. We first specified how many level we want in “nlevel”, and then the contour level is cimputed using the function shown above. The distance between the contour level is uniform, which is given in “step”.


call plenv(xmin,xmax,ymin,ymax,0,0)
call pllab('X','Y','Test 2D plot')

The first statement is to set up a standard window and draw box with min and max of the data values. The last two parameter are the “just” and “axis”. Just parameter determine how the axis will be scaled. The value 0 for “just” means that the x and y axes are scaled independently to use as much of the screen as possible. The “axis” parameter controls the drawing of the box around the plot. The value 0 means that it will draw box, ticks, and numeric tick labels.


call plcont(z,1,6,1,6,clevel,x2d,y2d)
call plend()

The first statement plot the contour. The fortran 95 function gives this options to call plcont:

plcont(z, kx, lx, ky, ly, clevel, tr?) or plcont(z, kx, lx, ky, ly, clevel, xgrid, ygrid)

We use the second option, where kx, lx are the range of x indices, and ky,ly are the range of y indixes. Since we are plotting all data, it starts with 1 and the last data is the number of grid point in x and y, which is 6.
clevel is the contour level which we have computed previously, while xgrid and ygrid are the 2D axis data. The latter means that xgrid and ygrid is a 2D array that gives the x and y position for the 2D array data.

The plot is shown in this image:
simple2d

Wiki for Corporate

my presentation during presentation skills workshop with the topic: Wiki for Corporate

Using Plplot from fortran in Kubuntu 8.04

I installed libplplot from Kubuntu Adept package manager, and chose “libplplot-fortran9” package.

To test the code, I created the following fortran program:

program testplplot2d
use plplot
implicit none
real(plflt),dimension(6) :: x,y
real(plflt)::xmin,xmax,ymin,ymax
x=(/1,2,3,4,5,6/)
y=x**2
write(*,*) y
call plinit()
xmin=1.0
xmax=6.0
ymin=1.0
ymax=40.0
call plcol0(1)
call plenv(xmin,xmax,ymin,ymax,0,0)
call pllab('X','Y','Test 1D plot')
call plpoin(x,y,9)
call plline(x,y)
y=x**3
call plpoin(x,y,9)
call plline(x,y)
call plend()

end program testplplot2d

and then compiled it using this command:

gfortran -o simple simple.f90 `PKG_CONFIG_PATH=/usr/lib/pkgconfig pkg-config --cflags --libs plplotd-f95`

When I run ./simple, it asks me to choose the output device, and I chose “1” which is the XWin display, and it plots as shown in the image below:
simple

Cleaning up “/” root directory

When I tried to install something, it turns out that my root directory usage is 100%, to check type “df -h”. I couldn’t figure out what was happening until I realize that I have updated so many Linux kernel. So I checked what is the latest Linux kernel version I have, and uninstall the rest. so the usage get back to about 80%. And I did clean up some of the programs that I don’t use anymore, and did:
sudo apt-get clean
sudo apt-get autoremove

But after that my installation still fail and said not enough disc space. This is weird since I have clean up already. When I type “df -h” I noticed the following line:
overflow 1.0M 104K 920K 11% /tmp

It turns out that I needed to reboot my system. The tmp directory seems to be mounted to a RAM (check this link). After reboot, I can install my program.

Installing Mayavi2 in Kubuntu/Ubuntu 8.04

Open the package manager, such as Adept or Synaptic. Add the following lines to the repository:

deb http://ppa.launchpad.net/gael-varoquaux/ppa/ubuntu hardy main
deb-src http://ppa.launchpad.net/gael-varoquaux/ppa/ubuntu hardy main

And reload the packages, search for “mayavi2”, and install. I encountered some problem (I forgot to take note the error message), it shows some import cannot be found. After I googled, it turns out that the cause of error is the old “enthought” eggs which I have installed previously, the forum suggested that I deleted these old files:
cd /usr/local/lib/python2.5/site-packages/
sudo rm -rf enthought.*

but after that I decided to try installation using “easy_install”, to see how easy it is. But the installation got error and said Sphinx cannot be found, so I installed sphinx first:

sudo apt-get install python-sphinx

And then tried the easy install by typing:

sudo easy_install "Mayavi[app]"

But even, after it successfully install, I got this error

Traceback (most recent call last):
File "/usr/local/bin/mayavi2", line 5, in
from pkg_resources import load_entry_point
File "/usr/lib/python2.5/site-packages/pkg_resources.py", line 2561, in
working_set.require(__requires__)
File "/usr/lib/python2.5/site-packages/pkg_resources.py", line 626, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python2.5/site-packages/pkg_resources.py", line 524, in resolve
raise DistributionNotFound(req) # XXX put more info here
pkg_resources.DistributionNotFound: enthought.mayavi==2.0.1a1

After I typed “which mayavi2” it turns out that it ran from /usr/local/bin with the old mayavi packages I tried to install, the new installation script done by easy_install is at /usr/bin/, so simply need to use the full path or copy to /usr/local/bin the new script. That ends my journey in installing mayavi2. 🙂

Updating OpenOffice 3.1.0 to 3.1.1 in Kubuntu 8.04

I found the update from OpenOffice 3.1.0 to 3.1.1 to be pretty simple. When I run an openoffice program, there is this Update notification on the top right of the window. So I clicked and it download the update. After finished downloading, I clicked “Install”, but as expected it didn’t work.

So I went to the folder where it downloaded the updates in *.tar.gz file. I unzipped the file using “tar xzvf file.tar.gz”, and go the folder OOXXXX something. In that folder, you will find a script “./update”. So simply run it using root privileges.

sudo ./update

And it updates your current openoffice 3.1.0 to 3.1.1. That’s it!