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

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: