Graphics: by J.S. Park Incheon National University
Graphics: by J.S. Park Incheon National University
Graphics
By J.S. Park
Incheon National University
Overview of Plotting
Plotting Process
• display data graphically.
• annotate and print graphs for presentations.
Creating a Graph
• line, bar, histogram, pie graphs, and 3-D graphs, such as surfaces, slice planes, and streamlines.
• Two basic ways to create MATLAB graphs:
- Use plotting tools.
- Enter commands in the Command Window or create plotting programs.
• combine both approaches
- a plotting command to create a graph and then modify the graph using the interactive
tools.
Annotating Graphs
• Annotations are the text, arrows, and other labels added to graphs
Basic Plotting Functions
plottools Show or hide the plot-editing tools for a figure. (plottools, plottools on, plottools off)
plot PLOT(X,Y) plots vector Y versus vector X.
semilogx Same as plot except logarithmic (base 10) scale is used for the X-axis
semilogy Same as plot except logarithmic (base 10) scale is used for the Y-axis
loglog Same as plot except Logarithmic scales are used for both the X- and Y- axes
plotyy Graphs with y tick labels on the left and right
PLOTYY(X1,Y1,X2,Y2) plots Y1 versus X1 with y-axis labeling on the left and
plots Y2 versus X2 with y-axis labeling on the right.
plot3 Plot lines and points in 3-D space. PLOT3() is a three-dimensional analogue of PLOT().
PLOT3(x,y,z), where x, y and z are three vectors of the same length, plots a line in
3-space through the points whose coordinates are the elements of x, y and z.
grid Grid lines. (grid, grid on, grid off)
title title('text') adds text at the top of the current axis.
xlabel xlabel('text') adds text beside the X-axis on the current axis.
ylabel ylablel('text') adds text beside the Y-axis on the current axis.
axis Control axis scaling and appearance.
axis([XMIN XMAX YMIN YMAX]) sets scaling for the x- and y-axes on the current plot.
axis([XMIN XMAX YMIN YMAX ZMIN ZMAX]) sets the scaling on the current 3-D plot.
axes AXES, by itself, creates the default full-window axis and returns a handle to it.
hold Hold current graph, so that subsequent graphing commands add to the existing graph.
(hold, hold on, hold off)
legend legend(string1,string2,string3, ...) puts a legend on the current plot using the specified strings as
labels.
legend off removes the legend from the current axes and deletes the legend handle.
subplot H = subplot(m,n,p), or subplot(mnp), breaks the Figure window into an m-by-n matrix of small axes,
selects the p-th axes for the current plot, and returns the axis handle.
Graph Components
MATLAB graphs display in a special window known as a figure.
x = -10:.005:40;
y = [1.5*cos(x)+4*exp(-.01*x).*cos(x)+exp(.07*x).*sin(3*x)];
plot(x,y)
title ('y = 1.5cos(x) + 4e^{-0.01x}cos(x) + e^{0.07x}sin(3x)')
xlabel('X Axis')
ylabel('Y Axis')
Script M-file
Script M-file
File name is fig_example.m
x = logspace(-2,3,1000);
y = sqrt(x);
semilogx(x,y)
grid
Using Basic Plotting Functions
Creating a Plot Plotting Multiple Data Sets in One Graph
x = 0:pi/100:2*pi; x = 0:pi/100:2*pi;
y = sin(x); y = sin(x);
plot(x,y) y2 = sin(x-.25);
xlabel('x = 0:2\pi') y3 = sin(x-.5);
ylabel('Sine of x') plot(x,y,x,y2,x,y3)
title('Plot of the Sine Function','FontSize',12)
The legend command provides an easy way
to identify the individual plots:
legend('sin(x)','sin(x-.25)','sin(x-.5)')
Specifying Line Styles and Colors
plot(x,y,'color_style_marker‘) Plotting Lines and Markers
plot(x,y,'ks')
plots black squares at each data point.
plot(x,y,'r:+')
plots a red-dotted line and places plus sign markers at each
data point.
plot(Z)
plot(real(Z),imag(Z))
For example,
>>t = 0:pi/10:2*pi;
>>plot(exp(i*t),'-o')
>>axis equal
subplot(1,1,1)
Controlling the Axes
The axis command provides options for setting the scaling, orientation, and aspect ratio of graphs.
t = -pi:pi/100:pi;
y = sin(t);
plot(t,y)
axis([-pi pi -1 1])
xlabel('-\pi \leq {\itt} \leq \pi')
ylabel('sin(t)')
title('Graph of the sine function')
text(1,-1/3,'{\itNote the odd symmetry.}')
plot3 for 3-D Plots
x=0:0.1:100;
y=sin(x);
z=cos(x);
plot3(x,y,z)
grid
0.5
-0.5
-1
1
0.5 100
80
0 60
-0.5 40
20
-1 0
Figure Tools
Figure Tools
Figure Toolbars
Plotting Tools
You can also start the plotting tools from the MATLAB prompt:
plottools
Plotting Tools
The plotting tools are made up of three independent GUI components:
• Figure Palette — Specify and arrange subplots, access workspace variables for plotting or editing, and
add annotations.
• Plot Browser — Select objects in the graphics hierarchy, control visibility, and add data to axes.
• Property Editor — Change key properties of the selected object.
You can also control these components from the Command Window, by typing the following:
figurepalette
plotbrowser
propertyeditor
Using Plotting Tools and MATLAB Code
t = 0:pi/20:2*pi;
y = exp(sin(t));
plotyy(t,y,t,y,'plot','stem')
xlabel('X Axis')
ylabel('Plot Y Axis')
title('Two Y Axes')
• For example, when you select two vectors named t and y, the Plot Selector initially
displays plot(t,y).
• If you click the down-arrow on the right side of the button, its menu opens to display
all graph types compatible with the selected variables.
Creating Graphs with the Plot Catalog
y = x4
is added to the
existing plot of
y = x3.
Changing the Type of Graph
1 Select both plotted series in the Plot Browser or
Shift+click to select them in the plot itself.
2 Select short dashes from the Line drop-down menu in the Property Inspector;
the line type of both series changes.
3 Select Stem from the Plot Type menu.
Modifying the Graph Data Source
>>x = linspace(-3*pi,3*pi,50); % Define 50 points between -3*pi and 3*pi
>>ys = sin(x); >>yc = cos(x);
>>figure
Data Source
>>x = linspace(-pi,pi,50);
>>y = sin(x);
>>area(x,y) % Make an area plot of x and y
Now, recalculate y at the command line:
>>y = cos(x)
Select the blue line on the plot. Select, x as the X Data
Source, y as the Y Data Source, and click Refresh Data.
The graph’s XData and YData are replaced.
Annotating Graphs for Presentation
Axis labels:
title ('y = 1.5cos(x) + 4e^{-0.01x}cos(x) …
Use the Figure Palette to annotate the plot. + e^{0.07x}sin(3x)')
Add a text box, using the Figure Palette.
Choose the Double arrow tool in the xlabel('X Axis')
Use the Property Editor to change the
Annotations section to draw a line between ylabel('Y Axis')
text style,.
two datatips.
Printing the Graph
File > Print Preview to view and modify
• Lines/Text tab,
- enter a line of text in the Header Text edit field
- font, style, and size of the header, date format
• The rulers along the left and top sides of the preview pane.
- stretch one edge of the plot, leaving the other edges in place.
- The inner handlebars let you move the plot up and down or left and right without stretching it.
- does not affect the figure itself, only the printed version of it.
• Layout tab
-change the size and
position of the plot on the
page using the buttons and
edit boxes.
-You can revert to the
original configuration by
clicking the Auto (Actual
Size, Centered) option
button,
-correct stretching and
shrinking by clicking Fix
Aspect Ratio.
Exporting the Graph
Creating a standard graphics file format of the graph (such as EPS or TIFF)
This example:
- Exports the graph as an EPS file
- The size : 4 inches wide and 3 inches high.
- All the text in the figure : a size of 8 points.
You can save the variables in your workspace by selecting Save Workspace As
from the figure File menu.
You can reload saved data using the Import Data item in the figure File menu.
- MATLAB data files, which have a .mat extension.
You can generate MATLAB code that recreates a figure and the graph it contains by selecting
Generate M-File from the figure File menu.
Creating Mesh and Surface Plots
About Mesh and Surface Plots
• You define a surface by the z-coordinates of points above a grid in the x-y plane.
• The mesh and surf plotting functions display surfaces in three dimensions.
• mesh produces wireframe surfaces that color only the lines connecting the defining points.
• surf displays both the connecting lines and the faces of the surface in color.
• The figure colormap and figure properties determine how the surface is colored.
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
mesh(X,Y,Z,'EdgeColor','black‘)
surf(X,Y,Z)
colormap hsv
colorbar
Making Surfaces Transparent
You can make the faces of a surface transparent to a varying degree.
Transparency (referred to as the alpha value) can be specified for the whole object or can be
based on an alphamap.
For example,
surf(X,Y,Z)
colormap hsv
alpha(.4)
These tools enable you to move the camera around the surface object, zoom, add lighting, and
perform other viewing operations without issuing commands.
Plotting Image Data
About Plotting Image Data
Two-dimensional arrays can be displayed as images, where the array elements determine brightness or color of
the images.
For example, the statements
>>load durer
>>whos
Name Size Bytes Class
X 648x509 2638656 double array
caption 2x28 112 char array
map 128x3 3072 double array
>>image(X)
>>colormap(map)
>>axis image
colormap(hot) adds some 21st century colorization to the 16th century etching.
See the colormap reference page for a list of other predefined colormaps.
Reading and Writing Images
imread function : read standard image files (TIFF, JPEG, BMP, etc)
>> imread('c:\windows\bamboo2.jpg');
>> image(ans)
>> axis image
imwrite(A,FILENAME,FMT)
A – variable name,
FILENAME – file name
FMT - format specified