MATLAB Graphics
▪ Line plot
▪ Bar graph
▪ Surface plot
▪ Contour plot
▪ MATLAB tutorial on 2D, 3D visualization tools as well as
other graphics packages available in our tutorial series.
Introduction to MATLAB 1
Line Plot
>> t = 0:pi/100:2*pi;
>> y = sin(t);
>> plot(t,y)
Introduction to MATLAB 2
Line Plot
>> xlabel(‘t’);
>> ylabel(‘sin(t)’);
>> title(‘The plot of t vs sin(t)’);
Introduction to MATLAB 3
Line Plot
>> y2 = sin(t-0.25);
>> y3 = sin(t+0.25);
>> plot(t,y,t,y2,t,y3) % make 2D line plot of 3 curves
>> legend('sin(t)','sin(t-0.25)','sin(t+0.25',1)
Introduction to MATLAB 4
Customizing Graphical
Effects
Generally, MATLAB’s default graphical settings are
adequate which make plotting fairly effortless. For
more customized effects, use the get and set
commands to change the behavior of specific
rendering properties.
>> hp1 = plot(1:5) % returns the handle
of this line plot
>> get(hp1) % to view line plot’s
properties and their values
>> set(hp1, ‘lineWidth’) % show possible
values for lineWidth
>> set(hp1, ‘lineWidth’, 2) % change line width
of plot to 2 Introduction to MATLAB 5
Save A Plot With print
>> x = magic(3); % generate data for bar graph
>> bar(x) % create bar chart
>> grid % add grid
• To add a legend, either use the legend command or via insert in
the Menu Bar on the figure. Many other actions are available in
Tools.
• It is convenient to use the Menu Bar to change a figure’s
properties interactively. However, the set command is handy for
non-interactive changes, as in an m-file.
• Similarly, save a graph via the Menu Bar’s File / Save as or
>> print –djpeg 'mybar' % file [Link] saved in current dir
Introduction to MATLAB 6
2D Bar Graph
>> x = magic(3); % generate data for bar graph
>> bar(x) % create bar chart
>> grid % add grid for clarity
Introduction to MATLAB 7
Use MATLAB Command or Function ?
• Many MATLAB utilities are available in both command and
function forms.
• For this example, both forms produce the same effect:
• >> print –djpeg 'mybar' % print as a command
• >> print('-djpeg', 'mybar') % print as a function
• For this example, the command form yields an unintentional
outcome:
• >> myfile = 'mybar'; % myfile is defined as a string
• >> print –djpeg myfile % as a command, myfile is treated as
text
• >> print('-djpeg', myfile) % as a function, myfile is treated as a
variable
• Other frequently used utilities that are available in both forms are:
• save, load Introduction to MATLAB 8
Surface Plot
>> Z = peaks; % generate data for plot; peaks returns
function values
>> surf(Z) % surface plot of Z
Try these commands also:
>> shading flat
>> shading interp
>> shading faceted
>> grid off
>> axis off
>> colorbar
>> colormap(‘winter’)
>> colormap(‘jet’) Introduction to MATLAB 9
Contour Plots
>> Z = peaks;
>> contour(Z, 20) % contour plot of Z with 20 contours
>> contourf (Z, 20); % with color fill
>> colormap('hot') % map option
>> colorbar % makecolorbar
Introduction to MATLAB 10