2D Plotting
MatLab Lecture THREE
Contents
• 2D plotting
2
Elements of a 2D plot
• Plot
• Axis labels
• Scale
• Titles
• Legend
• Text
• Line style
• Marker
3
2D plot
• Plot(x,y)
• Plot(x,y,’line specifiers’,’propertyname’,
propertyvalue)
4
Line Specifiers
Line Style Specifier
Solid (default) -
Dashed --
Dotted :
Dash-dot -.
5
Line Color Specifiers
Red R Magenta M
Green G Yellow Y
Blue B Black K
Cyan C White w
6
Marker Type Specifiers
Plus sign + Square S
Circle O Diamond D
Asterisk * Five-pointed star P
Point . Six-pointed start H
Triangle < or >
7
Examples
• Plot(x,y)
• Plot(x,y,’b’)
• Plot(x,y,’r:’)
• Plot(x,y,’>’)
• Plot(x,y,’r-.s’)
8
Specifying properties
• Plot(x,y,’m-
d’,’LineWidth’,2,’markersize’,12,’MarkerE
dgeColor’,’r’,’markerfacecolor’,’g’)
9
Plot the following data
X=1 2 3 4 5
Y=1 4 9 16 25
10
Plot of a Function
• Let us plot sin(x)
• 1st create vector x: x = linspace(-pi,20,pi)
• Compute the function: y=sin(x)
• Plot x and y : plot(x,y)
• Exercise: plot y=x^2+ e^x
11
The fplot Command
• fplot(‘function’,limits, line specifiers)
12
Ploting many functions in one figure
window
• Let: x = -2:0.01:4
• y = x^3 –x^2
• y1= 3x^2-2x
• y2=6x-2
• Plot(x,y,’r’,x,y1,’r:’,x,y2,’g-.’)
13
Ploting many functions in one figure
using hold on, hold off commands
• Let: x = -2:0.01:4
• y = x^3 –x^2
• y1= 3x^2-2x
• y2=6x-2
• Plot(x,y,’r’)
• Hold on
• Plot(x,y1,’r:’)
• Plot(x,y2,’g-.’)
• Hold off
14
Adding a line to existing plot
• Line(x,y,’linestyle’,’-’,’color’,’g’,’marker’,’d’)
• Plot previous data using line commands
15
Formatting a plot
• Labels
– Xlabel(‘string’)
– Ylabel(‘string’)
• Title
– Title(‘string’)
• Text
– Text(x,y,’text to be added’)
– Gtext(‘text as string’)
• Legend
– Legend(‘string1’,’string2’,’string3’,pos)
– Pos = -1, 0, 1 2 3 or 4
16
Formatting text in the formatting
commands
• \bf \it \rm
• \fontname{fontname}
• \fontsize{fontsize}
• Subscript a_b or a_{abc}
• Suprscript a^b or a^{abc}
17
Using greek letters in strings
• \alpha \beta \gamma \
theta
• \pi \sigma \phi \Phi
• \delta\Delta \Gamma
18
Formating text in plots
• Text (x, y,’text string’,’property name,’ property value)
• Rotation Scalar in degrees (default 0)
• FontAngle normal italic (normal)
• FontName availavle font name ( )
• FontSize scalar in points (default 10)
• FontWeight
• Color
• BackgroundColor
• EdgeColor
• LineWidth
19
axis
• Axis ( [xmin , mxax])
• Axis ( [xmin , mxax, ymin , ymax])
• Axis equal
• Axis square
• Axis tight
• Axis normal
20
GRID
• Grid on
• Grid off
21
Use Plot Editor
• Format a figure using plot editor
22
Semi-log and log-log plots
• Semilogy(x,y)
• Semilogx(x,y)
• Loglog(x,y)
• Caution
– Log of 0 does not exist
– Log of –ve number is complex
23
Other Common Plots
• Bar(x,y)
• Barh(x,y)
• Stairs(x,y)
• Stem(x,y)
• Pie(x)
• Hist(x)
• Hist(y,nbins) or hist(y,x)
• [N xout]=hist(y)
24
Polar plots
• theta=linspace(0,2*pi,100)
• R=3*sin(0.5*T).^2+T
• Polar (theta, R, ’r:d’)
25
Subplot (m,n,p)
(2,3,1) (2,3,2) (2,3,3)
(2,3,40 (2,3,5) (2,3,6)
Subplot(2,3,1), plot(x,y)
Subplot(2,3,2), plot(x,y)
…
26
Multiple axis
• x1 = [0:.1:40];
• y1 = 4.*cos(x1)./(x1+2);
• x2 = [1:.2:20];
• y2 = x2.^2./x2.^3;
• hl1 = line(x1,y1,'Color','r');
• ax1 = gca;
• set(ax1,'XColor','r','YColor','r')
27
Multiple axis
• ax2 = axes('Position',get(ax1,'Position'),...
• 'XAxisLocation','top',...
• 'YAxisLocation','right',...
• 'Color','none',...
• 'XColor','k','YColor','k');
• hl2 = line(x2,y2,'Color','k','Parent',ax2);
28
Coincident Grids
• xlimits = get(ax1,'XLim');
• ylimits = get(ax1,'YLim');
• xinc = (xlimits(2)-xlimits(1))/5;
• yinc = (ylimits(2)-ylimits(1))/5;
Now set the tick mark locations.
• set(ax1,'XTick',[xlimits(1):xinc:xlimits(2)],...
• 'YTick',[ylimits(1):yinc:ylimits(2)])
29
TiCk Marks
• set(gca,'YTick',[0 0.05 0.075 0.1 0.15 0.2 0.25])
• set(gca,'YTickLabel','0|0.05|Cutoff|0.1|0.15|0.2|0.25')
30
• figure(1)
• clf
• wide=12;
• set(gca,'Fontsize',10)
• set(gca,'Fontname','Times')
• set(gcf, 'PaperPositionMode', 'manual');
• set(gcf, 'PaperUnits', 'centimeters');
• set(gcf, 'PaperPosition', [0 0 12 8]);
• hold on, grid on, box on
• set(gca,'XScale','Linear') or set(gca,'XScale',‘log')
• t1=1:length(dp1);
• plot(t1,dp,'k:.','MarkerSize',4)
• xlabel('cake area load, g/m^2');ylabel('pressure drop, Pa')
• legend('cycle 1','cycle 2','cycle 3','Location','Best')
• hold off
31