MATLAB Part 4 - Plotting

Dave White

Figure 1 - 2D

f=@(x) 1./x;                           % y values of what is being plotted

Note on Continuity
The function f is continuous with inf inite domain
computers need discrete information

Discrete plotting

x=linspace(1,100,10)                   % x values of what is being plotted
y=f(x)
figure(1)                              % Initiates figure 1
plot(x,y)

Increase Resolution

x=linspace(1,100,1000)                 % x values of what is being plotted
y=f(x);                                % y values of what is being plotted
plot(x,y)

Basic Line Properties

plot(x,y,'+')                          % Plot unconnected plus siges
plot(x,y,':')                          % Plot connected dots
plot(x,y,'.')                          % Plot continuous line
plot(x,y,'k')                          % Plot black continuous line
plot(x,y,'r')                          % Plot red continuous line
plot(x,y,'r.')                         % Plot red points
plot(x,y,[.3 .3 .3]                    % RGB values
help plot                              % All properties

KEY VALUES

plot(x,y,'r.','MarkerSize',10)         % Change size of markers
plot(x,y,'r','LineWidth',10)           % Change size of line
plot(x,y,'ro','MarkerEdgeColor','b')   % Change collor of marker border

Labels

title('1/x')                           % Create title
xlabel('this is x')                    % Label x axis
ylabel('this is y')                    % Label y axis
legend('Responses')                    % Add legend with label 'Responses'

Zoom

xlim([0 40])                           % Set x axis limits
ylim([0 .4])                           % Set y axis limits
ylim('auto')
xlim('auto')

Multiple plots on same graph

g=@(x) 1./(x.^2)
z=g(x)
plot(x,z)                              % Erases what we've done
plot(x,y,'ro','MarkerEdgeColor','b')
hold on                                % "Don't erase"
plot(x,z)
legend('Responses1','Responses2')      % Add legend with 2 labels
hold off

DO EXCERCISE 4

Figure 2 - Subplots

Geneate several functions

figure(2)
x=-2:0.25:2;
y1=exp(x)
y2=log(x)
y3=abs(x)
y4=x.^2

Subplots

subplot(2,3,1)
plot(x,y1)
subplot(2,3,3)
plot(x,y2)
subplot(2,3,2)
plot(x,y4)
subplot(2,3,4)
plot(x,y3)

Spanning larger areas

subplot(2,1,2)
plot(x,y3)

Editing a subplot

subplot(2,3,2)
ylim([-3 1])

DO EXCERCISE 5

Figure 3 - 3D

Plotting a 3D function

h=@(x,y) x.*exp(-x.^2-y.^2)  % Same discretization problm before
x=linspace(-2,2,100);        % x values
y=linspace(-2,2,100);        % y values
z=h(x,y)
imagesc(z)                   % This doesn't look right...
size(z)                      % Actually visualizing accross the diagonal

Create Grid

[X,Y]=meshgrid(x,y);         % This distributes x and y
imagesc(X)
imagesc(Y)

Plot using Grid

figure(3)
z=h(X,Y)
size(z)
imagesc(z)                   % Third dimension is represented by color
colorbar
caxis([-.2, .2])
colormap winter
help graph3d

Surf

close                        % Close current figure
surf(z)                      % Third dimension is represented by color and space
                             % If resolution is too high, you can't see the surface
surf(z,'EdgeColor','none')
surf(z,'EdgeColor','none','FaceAlpha',.8)

Images

load('spine')                 % Loads in a variable X
size(X)                       % X is a matrix...
imagesc(X)                    % But an image!
colormap gray
axis image                    % Fix pixel dimensions

Plot on Images
You can plot on top of a figure
First notice that the y axis is upside down
Lets try and plot in the lower left corner

hold on
plot(300,80,'.r','MarkerSize',50)

Also, notice that our plotting indeces are treated backwards

imagesc(X)
plot(80,300,'.r','MarkerSize',50)
colormap gray

This didn't affect the color of our plotted points

DO EXCERCISE 8

Figure 4 - histogram

figure(4)
A=randn(1000,1)          % Normally distributed random numbers
histogram(A)             % Histogram, automatically setting the number of bins
histogram(A,10)          % Set number of bins to 10
help histogram

Figure 5 - bar

figure(5)
counts=[countsA; countsB]
bar(ctrsA,counts')
bar(ctrsA,counts',1)
bar(ctrsA,counts','stacked')
close all                 % Close all figures

figure(5)
plot(ctrsA,countsB,'k')
hold on
plot(ctrsB,countsB,'r')
close all

DO EXCERSISE 9