Basic Plotting

In [1]:
x=[0:2:18]
x =

     0     2     4     6     8    10    12    14    16    18


In [2]:
y=[0 0.33 4.1 6.3 6.85 11.2 13.2 13.96 16.3 18.2]
y =

         0    0.3300    4.1000    6.3000    6.8500   11.2000   13.2000   13.9600   16.3000   18.2000


In [5]:
plot(x,y)
title('Laboratory Experiment 1')
xlabel('Time, sec')
ylabel('Distance, ft')
legend('Data')
grid on

Plots with More Than One Line

In [8]:
x = 0:pi/100:2*pi;
y1 = cos(x*4);
y2 = sin(x);

plot(x,y1) % First way
hold on
plot (x,y2)
hold off

In [9]:
plot(x,y1,x,y2) % Second way

In [10]:
Y=[y1;y2]; % Third way
plot(x,Y)

Line, Mark, and Color Options

COLOR MARK LINE
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
ˆ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
In [12]:
x=[1:10];
y=[58.5 63.8 64.2 67.3 71.5 88.3 90.1 90.6 89.5 90.4];
plot(x, y, ':ok', x, y*2, '-xr', x, y/2, '-b')
legend('line 1', 'line 2', 'line3')
text(1,100,'Label plots with the text command')
xlabel('My x label')
ylabel('My y label')
title('Example graph for Lecture 3')
axis([0,11,0,200])

Axis Scaling, and Annotating Plots

  • axis : When the axis function is used without inputs, it freezes the axis at the current configuration. Executing the function a second time returns axis control to MATLAB
  • axis(v) : The input to the axis command (v) must be a four-element vector that specifies the minimum and maximum values for both the x and y axes for example, [xmin, xmax, ymin, ymax].
  • axis equal : Forces the scaling on the x and y axes to be the same.
  • legend('string1', 'string 2', etc) : Allows you to add a legend to your graph. The legend shows a sample of the line and lists the string you have specified.
  • text(x-coordinate, y-coordinate, 'string') : Allows you to add a text box to the graph. The box is placed at the specified x and y coordinates and contains the string value specified.
  • gtext('string') : Similar to text. The box is placed at a location determined interactively by the user by clicking in the figure window.

Subplots

subplot(m, n, p) : Allows you to subdivide the graphing window into a grid of m rows and n columns.

In [13]:
x = 0:pi/20:2*pi;
subplot(2,1,1)
plot(x, sin(x), '*r')
subplot(2,1,2)
plot(x, sin(2*x), '-b')

Polar Plots

In [14]:
x = [1 2 3 4 5];
y = [100 125 215 145 240];
polar(x,y)

Logarithmic Plots

In [20]:
x = 0:0.5:50;
y = 5*x.^2;

subplot(2,2,1)
plot(x,y)
title('Polynomial - linear/linear')
ylabel('y'), grid

subplot(2,2,2)
semilogx(x,y)
title('Polynomial - log/linear')
ylabel('y'), grid

subplot(2,2,3)
loglog(x,y)
title('Polynomial - log/log')
xlabel('x'), ylabel('y'), grid

subplot(2,2,4)
semilogy(x,y)
title('Polynomial - linear/log')
xlabel('x'), ylabel('y'), grid

Bar Graphs, Pie Charts, Histograms

  • bar(x) : When x is a vector, bar generates a vertical bar graph. When x is a two-dimensional matrix, bar groups the data by row.
  • barh(x) : When x is a vector, barh generates a horizontal bar graph. When x is a two-dimensional matrix, barh groups the data by row.
  • bar3(x) : Generates a three-dimensional bar chart.
  • bar3h(x) : Generates a three-dimensional horizontal bar chart.
  • pie(x) : Generates a pie chart. Each element in the matrix is represented as a slice of the pie.
  • pie3(x) : Generates a three-dimensional pie chart. Each element in the matrix is represented as a slice of the pie.
  • hist(x) : Generates a histogram.
In [21]:
x = [1 2 5 4 8];
y = [x;1:5];
subplot(2,2,1)
bar(x), title('A bar graph of vector x')
subplot(2,2,2)
bar(y), title('A bar graph of matrix y')
subplot(2,2,3)
bar3(y), title('A 3D bar graph of matrix y')
subplot(2,2,4)
pie(x), title('A pie chart of x')

In [22]:
x = [100 95 74 87 22 78 34 35 93 88 86 42 55 48];
hist(x)

x-y Graphs with Two y-axes

In [23]:
x = 0:pi/20:2*pi;
y1 = sin(x);
y2 = exp(x);
subplot(2,1,1)
plot(x, y1 ,x ,y2)
title('Single y-Axes Scaled')
subplot(2,1,2)
plotyy(x, y1, x, y2)
title('Two y-Axes Scaled')

Function Plots

fplot : Allows you to plot a function without defining arrays of corresponding x and y values.

In [26]:
fplot('sin(x)',[-2*pi,2*pi])
Warning: Char input to fplot will be removed in a future release. Use fplot(@(x)sin(x)) instead.
> In fplot (line 101)

Three-Dimensional Plots

  • plot3(x,y,z) : Creates a three-dimensional line plot.
  • comet3(x,y,z) : Generates an animated version of plot3.
  • mesh(z) or mesh(x,y,z) : Creates a meshed surface plot.
  • surf(z) or surf(x,y,z) : Creates a surface plot; similar to the mesh function.
  • shading interp : Interpolates between the colors used to illustrate surface plots.
  • shading flat : Colors each grid section with a solid color.
  • colormap(map-name) : Allows the user to select the color pattern used on surface plots.
  • contour(z) or contour(x,y,z) : Generates a contour plot.
  • surfc(z) or surfc(x,y,z) : Creates a combined surface plot and contour plot.
  • pcolor(z) or pcolor(x,y,z) : Creates a pseudo color plot.