numEclipse - An Eclipse based workbench for

Numerical Computing

Home Downloads Resources Links

 

Data visualization or plotting is an important part of scientific computing. Our initial intent was to write Java2D based plotting APIs. But we quickly realized that not only this is an enormous task itself but also there is no point in reinventing the wheel. There are already a number of open source projects providing excellent APIs for plotting. So we decided to use an existing project and provide an interface within numEclipse. There are number of applications and APIs available but our objective was to choose something similar to MATLAB. We started looking at PLPlot first, it is a set of plotting functions written in C. This project also provides java binding to the C functions. Unfortunately, this project is more geared towards linux / unix users. We first time compiled the jni enabled dll on WindowsXP and we came across a lot of problems. PLPlot functions have their own windows management, once a graph is plotted by a java program through binding, it has no control over the plot window. Also we discovered that you could only have one plot at a time which is not acceptable for our purpose. Finally, we decided to take the approach of octave and provided an interface to gnuplot. It is an excellent tool for scientific plotting. It has been developed over a long period of time. We are using version 4.0 and it is very mature and stable. We provide this interface to gnuplot as a built-in toolbox. We are hoping that some users will try to write their own toolbox to other visualization APIs or applications.

 

5.1 Function Plot

 

We will start with a very simple plot as shown in the following example.

 

>> fplot(“sin(x)”)

 

fplot method is used to plot a mathematical function. It can only plot the functions recognized by gnuplot. Fortunately, it has a very good collection of built-in functions. Unlike MATLAB in this method the function name is not enough i.e. “sin”. We also need to provide the arguments to the function i.e. “sin(x)”. The output is follows.

 

Figure: 4.1

 

However, the numbers of supported functions are limited but it does not stop a user to define new functions using algebraic combination of existing functions. For example

 

>> fplot (“(sin(x)-cos(x))/ (sin(x)+cos(x), “[-pi:pi]”, “[-150:150]”)

 

In this example, we also define the x-range and y-range of the function plot. Please note that the range arguments are strings and not arrays unlike MATLAB. We also use the following commands to label the plot.

 

>> xlabel (“x -->”)

>> ylabel (“y -->”)

>> title (“My First Plot”)

 

Figure: 4.2

 

xlabel, ylabel, zlabel and title are common methods for any kind of plot we will discuss in the following sections. We can also control the line type and color of the function plot as shown in the following.

 

>> grid on

>> hold on

>> fplot (“(sin(x)-cos(x))/ (sin(x)+cos(x), “[-pi:pi]”, “[-150:150]”, “bx”)

>> fplot (“100*sin(x)”)

>> hold off

>> grid off

 

In this example, we show how to overlay one plot over another by using hold on and hold off commands. Also, we show how to add a grid to the plot. The last argument of the fplot method controls the plot format. The first character represents the color and second character represents the line type of the plot.

 

Color

Line Type

r (red)

*

g (green)

+

b (blue)

-

m (magenta)

.

 

o

 

x

 

 

 

Figure: 4.3

 

5.2 Line Plot

 

5.2.1 2D Line Plot

The most commonly used line plot method is plot. plot(X) will plot the elements of X versus their index if X is a real vector. If X is complex vector then it will plot the imaginary component of vector X versus the real components. In case X is a real matrix then plot(X) will draw a line plot for each column of the matrix versus the row index. In case of complex matrix it will plot the imaginary components of the column versus their corresponding real components. plot(X, Y) will plot elements of vector Y versus elements of vector X. In case of complex vectors the imaginary components will be ignored and only real part of the vector elements will be used for plotting. We can add one more string argument to define the format of the plot, e.g. plot(X, “g+”), plot(X, Y, “bx”). The format is a two letter string where the first letter represents the color and second defines the line style. The possible values for color and line style are described in the previous section. A simple line plot example is given in the following.

 

>> hold on;

>> t = 0:0.01:1;

>> x = sin(6*pi*t);

>> plot(t, x, “b+”);

>> y = cos(6*pi*t);

>> plot(t, y, “rx”);

 

Figure 4.4

 

The above graph shows the points corresponding to sin function as blue plus ‘+’ symbols and points for cos function as red cross ‘x’ symbols.

We can narrow down the plot to a smaller x- and/or y-range by using the following command.

>> axis([0.4, 0.6, -0.5, 0.5]);

 

It is a common method applicable to any plotting method within numEclipse. It allows to redefine the range of the axes. The different method signatures are given in the following.

 

axis([xmin, xmax])

axis([xmin, xmax, ymin, ymax])

axis([xmin, xmax, ymin, ymax, zmin, zmax])

 

xmin                minimum value of x axis

xmax               maximum value of x axis

ymin                minimum value of y axis

ymax               maximum value of y axis

zmin                 minimum value of z axis

zmax                maximum value of z axis

 

Vector elements are separated by comma in numEclipse unlike MATLAB.

 

Figure: 4.5

 

We have three more methods very similar to plot method. These methods work in a very similar way as plot method except the way they plot the axes.

 

semilogx           It uses log (base 10) scale for the X-axis.

semilogy           It uses log (base 10) scale for the Y-axis.

loglog               It uses log (base 10) for both X and Y axes.

 

 

 

 

>> t = 0:10;

>> for i = 1:length(t)

x(i) = 10^t(i);

end;

>> y = log(x);

>> semilogx(x, y, “+”)

>> grid on

 

Figure: 4.6

 

Note the equi-distant vertical grid lines even though the difference in the x values is exponent of 10. The log function used in the above script is based on e rather than 10.

 

5.2.2 3D Line Plot

The 3D version of previously discussed plot method is plot3. It takes three vectors as arguments representing the x, y and z components of 3-dimensional line. A fourth String argument could be added to define the format of the line in the same manner as plot method. At this point, this method does not support matrix arguments, unlike MATLAB.

 

>> t = 0:pi/100:10*pi;

>> x = sin(t);

>> y = cos(t);

>> plot3 (x, y, t);

 

Figure: 4.7

 

 

5.2.3 Polar Plot

Polar plot method polar (theta, rho) draws a plot using polar coordinates of angle theta (in radians) versus the radius rho. Like the previous methods, we can also add a third String argument defining the format of line color and style as described previously.

 

>> t = 0:pi/100:10*pi;

>> polar(t, cos(4*t), “bx”)

 

Figure: 4.8

 

 

5.2.4 Bar Chart

Bar chart method bar(X) makes a bar graph of elements of vector X as shown in the following example.

 

>> bar(1:10)

 

Figure: 4.9

 

 

Method bar(X, Y) draws a bar graph of elements of vector Y versus the elements of vector X, provided elements of X are increasing in the ascending manner.

 

 

5.2.5 Histogram Plot

Histogram plot is a commonly used plot in any scientific presentation. The method hist(X) draws a histogram of the vector data X. It divides the data into 10 bins between the minimum and maximum values of X.  

 

>> x = rand(1, 10)

0.1136 0.8088 0.7512 0.6826 0.7308 0.1047 0.4745 0.0916 0.1699 0.4284

>> hist(x)

 

Figure: 4.10

 


5.2.6 Stairs Plot

It is not a very common plot. Method stairs(X) makes the stairstep plot of the elements of vector X versus the element index. Similarly method stairs(X, Y) draws the stairstep plot of elements of vector Y versus elements of vector X.

 

>> x = rand(1,10)

0.4967 0.1981 0.2225 0.7743 0.4655 0.33     0.5885 0.9173 0.1439 0.3495

>> stairs(x)

 

Figure: 4.11

 


5.3 Mesh Surface

 

Mesh surface is a popular 3D plot for scientific visualization. Method mesh(X, Y, Z) plots a meshed surface defined by the matrix argument Z such that vectors X and Y define the x and y coordinates. This method without the first two arguments will use the row and column index of the matrix Z to plot the mesh.

 

>> x = -2:0.05:2;

>> y = x;

>> for i=1:length(x)

            for j=1:length(y)

                        z(i, j) = 2 * (x(i)^2 + y(j)^2) * exp(-x(i)^2 - y(j)^2);

            end;

end;

>>mesh(x, y, z)

 

Figure: 4.12

 

 

A variation of this method is meshc(X, Y, Z). This method enhances the mesh plot by adding contours. In the absence of first two arguments it works the same way as method mesh.

 

>> meshc (x, y, z)

 

Figure: 4.13

 

 

Sometimes we are only interested in the topography of the surface rather than the complete mesh plot. There is another method contour, which plots only the contour of the surface. It takes same arguments as mesh or meshc.

 

>> contour (x, y, z)

 

Figure: 4.14

 

 


5.4 Multiple Plots

 

Under development