=@(x) 1./x; % y values of what is being plotted f
Note on Continuity
The function f is continuous with inf inite domain
computers need discrete information
Discrete plotting
=linspace(1,100,10) % x values of what is being plotted
x=f(x)
yfigure(1) % Initiates figure 1
plot(x,y)
Increase Resolution
=linspace(1,100,1000) % x values of what is being plotted
x=f(x); % y values of what is being plotted
yplot(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
=@(x) 1./(x.^2)
g=g(x)
zplot(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
Geneate several functions
figure(2)
=-2:0.25:2;
x=exp(x)
y1=log(x)
y2=abs(x)
y3=x.^2 y4
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
Plotting a 3D function
=@(x,y) x.*exp(-x.^2-y.^2) % Same discretization problm before
h=linspace(-2,2,100); % x values
x=linspace(-2,2,100); % y values
y=h(x,y)
zimagesc(z) % This doesn't look right...
size(z) % Actually visualizing accross the diagonal
Create Grid
,Y]=meshgrid(x,y); % This distributes x and y
[Ximagesc(X)
imagesc(Y)
Plot using Grid
figure(3)
=h(X,Y)
zsize(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)
=randn(1000,1) % Normally distributed random numbers
A% Histogram, automatically setting the number of bins
histogram(A) ,10) % Set number of bins to 10
histogram(Ahelp histogram
figure(5)
=[countsA; countsB]
countsbar(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