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

Advertisements

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: