Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
MET 164
Matlab Programming - Plotting
MET 164
Today we are going to learn details about plotting
MET 164
The “close all” command at the beginning of a script will
close all of the previous figures
figure (number): will open a specific figure that you can use to
plot data
plot: will plot the data in the current figure (or open a figure if
there are none open)
>> t = [0:0.1:5];
>> y = sin(t);
>> figure(1)
>> figure(2)
>> plot(t,y)
MET 164
Matlab has a default sequence of colors for the plots that
are used, unless you define the colors
b blue plot(x,y,’b’)
g green plot(x,y,’g’)
r red plot(x,y,’r’)
c cyan plot(x,y,’c’)
m magenta plot(x,y,’m’)
y yellow plot(x,y,’y’)
k black plot(x,y,’k’)
w white plot(x,y,’w’)
Without the letters, the plot function will always start with the color at the
top of this list and work down
MET 164
Matlab always uses solid lines, unless indicated to use a
different type of line
- solid plot(x,y,’-’)
: dotted plot(x,y,’:’)
-. dashdot plot(x,y,’-.’)
-- dashed plot(x,y,’--’)
These can be used in combination with the colors as well
plot(x,y,’--g’) or plot(x,y,’g--’) produces a figure with a green
dashed line
MET 164
The specific data points used for the plot can be
highlighted with specific characters
. point o circle
x x-mark + plus
* star s square
d diamond v triangle (down)
^ triangle (up) < triangle (left)
> triangle (right) p pentagram
h hexagram
These can be used in combination with the colors/symbols
plot(x,y,’x--g’) or plot(x,y,’gx--’) produces a figure with a green
dashed line with an “x” at all of the data points
MET 164
We can code labels, titles, etc. for the figure within the
script or using the drop down menus from the figure
MET 164
We can code labels, titles, etc. for the figure within the
script
xlabel(‘length (cm)’)
ylabel(‘frequency (Hz)’)
title(‘Data Model Comparison’)
MET 164
The font and font size of the text can be adjusted for the
figures as well
A handle contains all of the information that will be used for the
text
set(gca,'Fontname','timesNewRoman','Fontsize',12)
set(gca,'Fontname','timesNewRoman','Fontsize',20)
gca is a function to “get current axis” that returns a handle to the axis
MET 164
The font and font size of the text can be adjusted for
each part as well
xlabel('length (cm)','fontsize',14)
ylabel('frequency (Hz)','fontsize',18)
title('Data Model Comparison','fontname','Algerian','fontsize',20)
MET 164
Legends are included in plots to help define the different
types of data
legend('theoretical','experimental')
set font name and size using the
handle
location can be used to place the
legend in a specific location in the
text
there are 18 specific locations where
the legend can be placed including
best
compass locations
legend('theoretical','experimental‘,’location’,’southwest’)
MET 164
Text can be placed at any location on the plots as well
that is not part of the legend
text(15,100,'under the plot','fontsize',16)
the figure handle will not adjust the font name or size
MET 164
We can also define the size of each axis of the plot
axis: command that allows you to
adjust your axis
tight
equal
square
off
axis([xl xu yl yu])
axis([0 60 75 300])
axis([xl xu yl yu zl zu]): for 3D
plots
MET 164
The tick marks of each axis can be controlled as well
and grid lines can be included
include the tick marks in the handle
‘xtick’
‘ytick’
turn on the grid grid on
set(gca,'Fontname','timesNewRoman','Fontsize',14,'xtick',[10:5:60])
MET 164
Multiple graphs may be put on the same figure in two
different ways
At the same time
plot(length,theory,length,experimental)
follows the color sequence
Or at different times
plot(length,theory)
hold on
plot(length,experimental)
the color needs to be defined or each subsequent plot will start with ‘blue’
MET 164
The line width for the different plots can be changed as
well
But, each plot needs to be plotted separately with the ‘hold on’ command
plot(length.*100,theory_3,'--','LineWidth',6)
hold on;
plot(length.*100,experimental_3,'r','LineWidth',2)
MET 164
We can also place multiple different plots in the same
figure
subplot(2,1,1)
plot(length,theory)
hold on
plot(length,experimental)
subplot(2,1,2)
plot(length2,theory2)
hold on
plot(length2,experimental2)
MET 164
We can also graph different axis types
plotyy: places two different y-axes
semilogx: x-axis is log scale
semilogy: y-axis is log scale
loglog: both x and y axes are log scale
hist: histogram chart
pie: pie chart
polar: polar graph
bar: bar chart
MET 164
We can also graph 3D plots and surface plots as well
>> plot3(t,y,y1)
>> grid on
include the third axis as
well
zlabel
axis command
01
23
45
-1
-0.5
0
0.5
1-1
-0.5
0
0.5
1
MET 164
In conclusion
There are numerous types and ways to plot with Matlab
The help files and google are friends to determine the ways to
plot what you need