Plotting graphs

 

Line Plots

X = [0, 1, 1.5, 2, 3]
Y = [1, 0, 0.25, 1, 4]
plot(X, Y)
03-plot
Y = [1 2 3 4 5 6 7 8 9];
plot(Y, '--*')
Y = [1; 2; 3; 4; 5; 6; 7; 8; 9];
plot(Y, '--*')

Both the above plot commands give the same output:

04a-plot-vector-1
Y = [2 4 6 8 10];
% X values take 1:size(Y)
plot(Y, '--*')
X = [1 2 3 4 5];
Y = [2 4 6 8 10];
plot(X, Y, '--*')

Both the above plot commands give the same output:

04b-plot-vector-2
Z = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
>>
   1    2    3
   4    5    6
   7    8    9
  10   11   12
plot(Z, '--*')
05-plot-matrix

The same plot as above can be drawn using:

plot(Z(:,1), '--*');
hold on;
plot(Z(:,2), '--*');
plot(Z(:,3), '--*');

NOTE: The command hold on is used for plotting two or more plots on a single graph.

% close the graph
close;

% clear figure
clf;

Plotting histogram

P = randi([0,50],1,10000)
hist(P)
06-hist-vector