Changing background and front color in plplot library

The default background color in plplot library is black while the frontcolor is red. To do this, use plscolbg and plcol0 function. The code in fortran is:


call plscolbg(255,255,255)
call plinit()
call plcol0(8)

Remember that plscolbg must be called before plinit. The above code change the background color to white (rgb=255,255,255) and the front color to brown (8, refer to manual).

Note also that changing the background color will affect the color map 1 which is used for plotting continuous data. The reason is that color map 1 range from blue to red passing through the background color (default).

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

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