# JMathLabTutorial:Plots

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Contents

# Plotting

Data may be graphed using the plot(x,y)-function, x and y being equalsized vectors, which denote the coordinates of the data points to be plotted. A third optional argument plot(x,y,option) specifies plot options like colors and symbols. The graphic may then be decorated (axis, title) and exported as encapsulated-postscript file for inclusion in text documents. With hold (or hold on) the graphic gets locked so that subsequent plot commands use the same window. Repeating hold (or hold off) deletes the graphic.

The method:

plot(x,y [option])

plots x versus y using option x and y are equalized vectors. option is a string, specifying color (one of r,g,b,y,m,c,w,k) and symbol (one of +,*,o,x). Default: blue lines.

Logarithmic and semi-logarithmic plots are provided with the functions loglog, linlog and loglin.

Here are the major plot methods:

Name(Arguments) Function
plot(x,y [option]) Plot x versus y
loglog(x,y [option]) logarithmic plot
linlog(x,y [option]) semi-logarithmic plot
loglin(x,y [option]) semi-logarithmic plot
print(name ) write graphic in eps-file

Let as show a simple example of plotting sin(x) between 0 and 10 with a step 0.1

x=0:0.1:10; y=sin(x)
plot(x,y)

However, one can overlay these plots using the "hold" method:

x=0:0.1:10
plot(x,sin(x))
hold
plot(x,x*x)

You can plot symbols as well:

x=0:0.1:10; y=sin(x); z=cos(x)
plot(x,y, "or")
hold % hold this plot for overlay
plot(x,y, "r")
plot(x,z, "+b")

Run this plot to see its output.

# Plotting data and function

Now let us draw data points with errors and overlay a Gaussian function $y = A*\exp(-((x-x0)/\sigma)^2)$. First we build a function "fit", draw data points and overlay the Gaussian function at fixed values [1.4912 3.9911 1.4481 ]

function y=fit(a,x) % build function to fit data
y=a(1)*exp(-(x-a(2)).^2/a(3)^2); end;

x = 0:0.5:10; % build data
for i=1:length(x),y(i)=1.5*exp(-(x(i)-4)^2/2)+rand(1)/5-0.1; end;
e = 0.1 * ones( 1, length(x) );  % errors

errorbar(x,y,e,'+'); % draw error bars
hold
xi=0:0.1:10;
yi=fit([ 1.4912  3.9911  1.4481 ],xi);
plot(xi,yi,'g');
plot(x,y,'r*');

# Examples

Example 1 let us plot the function: $y=\frac{1}{1+2x^2}$ in the range x=0.01...100 linear and logarithmic. The code is below:

x=0.01:0.01:100; y=1./(1+0.5*x.*x); plot(x,y)
x=0.01:0.01:100; y=1./(1+0.5*x.*x); loglog(x,y)

Example 1a let us plot the function cos(x):

x=-10:0.1:10;
plot(x,cos(x));

Example 2: Display of Lissajous-figures: From the vector t=0:0.1:4*pi; create the trigonometric expressions x=sin(0.5*t+1); and y=cos(1.5*t);. The plot x vs. y is called Lissajous-figure. Create different figures by variating the constants 0.5,1,1.5 in the definition. Partial solution:

t=0:0.1:4*pi;
x=sin(0.5*t+1);
y=cos(1.5*t);
plot(x,y)

Example 3 Calculate the first 100 elements of the sequences

$x_n = \frac{n-1}{n}; x_n=\frac{n+1}{n}; x_n=\frac{n+(-1)^n}{n}$

Plot $x_n$ versus $n$ using the command plot(n, xn). Variate the plotoptions (colors, symbols).

n=1:100;
x1=(n-1)./n; x2=(n+1)./n; x3=(n+(-1).^n)./n;
plot(n,x1)        % Standard: blue, lines
hold % Current plot held.
plot(n,x2,'r')    % Color:  r,g,b,c,m,y,w.
plot(n,x3,'g')

Example 4 Plot the data points of the following table using plot and colored symbols. Calculate the linear regression using polyfit, and plot the regression line in the same graph. Add title and labels, and export the graphic to a file suitable for inclusion in a text document.

x  0 	  1    2    3 	  4    5     6     7 	8 	9
y -3.1 -0.7  1.8  4.1 	6.2  8.9  11.3 	13.5 	16 	18.3

x=0:9;
y=[-3.1,-0.7,1.8,4.1,6.2,8.9,11.3,13.5,16,18.3];
plot(x,y,'+r')    % Symbol: o,x,+,*
hold % Current plot held.
plot(x,polyval(polyfit(x,y,1),x)) % Regression
xlabel('x-Axis')
ylabel('y-Axis')
title('Title')
print('graph.eps')

# Plot2D

The method plot2d() is used to plot functions and arrays in 2D. One can add title, labels, change the minimum and maximum ranges for X-axis and Y-axis. You can also apply different styles.

First, let us create a canvas and prepare for plotting of data.

plot2d()

There are several methods associated with it:

• clear2d - clear the canvas from data
• export2d(file) - export the plot to the vector format images.

## Plotting analytic functions

To draw a function, first create a function using a symbolic variable "x", then create a canvas and then use draw2d(o) to plot the function.

syms x
a=x*cos(x)
plot2d()
draw2d(a)

You can set the X and Y ranges for the plotted functions. In this example we differentiate an analytic function and then plot in in the given range [0-10] in X and [-1-10] in Y:

syms x % symbolic x
y=diff(x*cos(x),x) %  differentiate x*cos(x)
plot2d('minx=0;maxx=10;miny=-1;maxy=10')  % make canvas
draw2d(y) % plot the result

Plotting of special functions is also possible; they all can accept not only a single value but vectors.

x=0.01:0.05:1 % create a vector from 0.01 to 1 with step 0.005
y=erf(x)
printf('%f',y)
plot2d()  % make canvas
draw2d(x,y)

Here we plotted the error function.

One can draw multiple functions, setting a new color, style and name for the second function:

syms x
a=x*cos(x)
plot2d()
draw2d(a)
b=x*sin(x)
draw2d(b,'name=newfunction;linestyle=2;color=red;')

It should be noted that functions can also be plotted by parsing a string:

plot2d()
draw2d('x*cos(x)')

## Plotting data

Let as show a simple example of plotting sin(x) between 0 and 10 with a step 0.1

x=0:0.1:10; y=sin(x)
plot2d('titlex=X;titley=Y;')
draw2d(x,y)
draw2d(x,y*2,'name=newdata;')

You can draw the second plot with a different style:

x=0:0.1:10; y=sin(x)
plot2d()
draw2d(x,y)
draw2d(x,y*2,'style=l;color=blue;')

## Plotting data with errors

You can plot data with errors. Errors are shown as lines, and can be attributed to Y-values (asymmetric upper and lower errors) and to X-values (left and right error). Errors can be asymmetric.

In this example, we plot data with symmetric errors on Y (which is 40% of the y-values). The array "z" represents symmetric errors on the Y-values.

x=0:1:10; y=sin(x);
z=ones(1,length(x)) * 0.3 % vector with errors. All have 0.3 value
plot2d()
draw2d(x,y,z)

In this example, we plot data with asymmetric errors on Y (which is 40% and 80% of the y-values). In this example, the upper error on Y is 40% of the value y, while lower error is 80% of the value y:

x=0:1:10; y=sin(x);
z=ones(1,length(x))  % errors with value 1
plot2d()
draw2d(x, y, 0.4*z, 0.8*z) % errors are asymmetric

Now let us plot errors on X and Y. We assume that left error is 30% of X value, and right value is 60% of x value.

x=0:1:10; y=10*sin(x);
z=ones(1,length(x))  % errors with value 1
plot2d()
draw2d(x, y, 0.3*z, 0.6*z, 3*z, 5*z)

Finally, here is a custom example:

x=[1, 2]; y=[10, 20]
exLeft=[0.5, 0.5]
exRight=[0.3, 0.3]
eyUpper=[5, 5]
eyLower=[2, 2]
plot2d()
draw2d(x, y, exLeft, exRight, eyUpper, eyLower)

## Plotting options

You can pass the following options to the "draw2d" method:

## Exporting plots into images

There are several methods associated with it: clear2d (clear the canvas from data) and export2d(file), to export the plot to the vector format image, such as PDF, EPS, as well as PNG and JPG. When exporting images to the file, use the file extension to define image format. For example, export2d("image.pdf") indicates that the plot will be saved into the PDF format, export2d("image.eps") tells that the plot will be saved into the high-quality vector format using EPS/PS format.

# Plot3D

You can plot 3D surface plots (in case of functions) or show data points in 3D using plot3D method. The plots are fully interactive and one can rotate and zoom-in particular regions.

First, let us create a canvas and prepare for plotting of data in 3D.

plot3d()

There are several methods associated with it: clear3d (clear the canvas from data) and expert3d(file), to export the plot to the vector format image, such as PDF, EPS, as well as PNG and JPG. You can also put title, label and change the minimum and maximum ranges for X-axis and Y-axis. You can pass the following options to this canvas:

## Plotting functions in 3D

Functions can be shown as surface plots in 3D. You will need to define symbolic variables x and y, and then construct symbolic function. The pass it to the method draw3d: Run this example to find how it works. It will show a pop-up canvas with a surface plot in 3D:

syms x,y
plot3d('titlex=X;titley=Y;titlez=Z;')
f=x*y*cos(x*y)
draw3d(f)

Alternatively, one can pass a string which specifies a function using "x" and "y" as variables. Run this example to see how it works:

plot3d('titlex=X;titley=Y;titlez=Z;')
draw3d('x*y*sin(x)')

This generates interactive surface plot in 3D. The ranges of X,Y,Z can be specified as option for the method plot3d.

## Plotting data in 3D

Data may be graphed using the plot3d function as well. In this case you need to pass 3 vectors

Let as show a simple example of plotting sin(x) between 0 and 10 with a step 0.1

x=0:0.3:10; y=sin(x); z=cos(x);
plot3d('titlex=X;titley=Y;titley=Z;')
draw3d(x,y,z)

Note that it is convenient to set X,Y,Z ranges during the canvas creating step, in this case the axis labels will be nicely formatted (unlike the example above).

## Exporting plots to images

To clear the plot, use "clear3d" method. One can export the plot in PDF, EPS, PS, PNG, JPG image formats using the method export3d(file) method. For example, export3d("image.eps") will export the graph to EPS/PS format using high-quality vector graphics.

## Drawing options

You can pass the following options to the "draw3d" method: