You are a guest. Restricted access. Read more


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)

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


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(-1)

1) 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
yi=fit([ 1.4912  3.9911  1.4481 ],xi);

====== 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):


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:


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).

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.  

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

plot(x,y,'+r')    % Symbol: o,x,+,*
hold % Current plot held.
plot(x,polyval(polyfit(x,y,1),x)) % Regression

jmathlab/plotting/plot.txt · Last modified: 2015/05/10 17:18 (external edit)
Back to top
CC Attribution-Share Alike 3.0 Unported = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0