Special Plot Types Matlab Help

Special Plot Types

In this section we show how to obtain logarithmic axes; how to change the default tick-mark spacing and labels; and how to produce other specialized plots. Logarithmic Plots Thus far we have used only rectilinear scales. However, logarithmic scales are also widely used. (We often refer to them with the shorter term, log scale.) Two common reasons for choosing a log scale are (1) to represent a data set that covers a wide range of values and (2) to identify certain trends in data. As you will see, certain types of functional relationships appear as straight lines when plotted using a log scale. This method makes it easier to identify the function. A log-log plot has log scales on both axes. A semilog plot has a log scale on only one axis. For example Figures 5.3-1 and 5.3-2 show plots of the function:

Capture

The first plot uses rectilinear scales, and the second is a log-log plot. Because of the wide range in values on both the abscissa and ordinate, rectilinear scales do not reveal the important features. It is important to remember the following points when using log scales:1. You cannot plot negative numbers on a log scale, because the logarithm of a negative number is not defined as a real number. 2. You cannot plot the number 0 on a log scale, because loglo 0 = In 0 = -00. You must choose an appropriately small number as the lower limit on the plot.

Rectilinear scales cannotproperly display variations over wide ranges.

Rectilinear scales cannotproperly display variations over wide ranges.

A log-log plot can display wide variations in data values

A log-log plot can display wide variations in data values

3. The tick-mark labels on a log scale are the actual values being plotted; they are not the logarithms of the numbers.For example, the range of x values in the plot in Figure 5.3-2 is from lO-1 = 0.1 to lO2 = 100. 4. Equal distances on a log scale correspond to multiplication by the’ same constant (as opposed to addition of the same constant on a rectilinear scale). For example, all numbers that differ by a factor of 10are separated by the same distance on a log scale. That is, the distance between 0.3 and 3 is the  same as the distance between 30 and 300. This separation is referred to as ‘ a decade or cycle. The plot shown in Figure 5.3-2 covers three decades in x (from 0.1 to 100)and four decades in y and is thus called a four-by-three-cycle plot. 5. Gridlines and tick marks within a decade are unevenly spaced. If 8 gridlines or tick marks occur within the decade, they correspond to values equal to 2, 3, 4, … , 8, 9 times the value represented by the first gridline or tick mark of the decade.
MATLAB has three commands for generating plots having log scales. The appropriate command depends on which axis must have a log scale. Follow these rules:

1. Use the loglog (XiY) command to have both scales logarithmic.
2. Use the semilogx (X, Y) command to have the x scale logarithmic and the y scale rectilinear. ‘
3. Use ‘the se’milogy (x ,Y) command to have the y scale logarithmic and the:x scale rectilinear,
Table,.5’.3-1 summarizes these functions. For other 2D plot types, type help  specgraph.
V!e can plot multiple curves with these commands just as with the plot command: In addition, we can use the other commands, such as grid. x label, and axis. ‘in the same manner.

Specialized plot commands

Specialized plot commands

Two data sets plotted on four types of plots

Two data sets plotted on four types of plots

Figure 5.3-3 shows plots made with the plot command and the three logarithmic plot commands. The same two data sets were used for each plot. The , session follows

» [1,1.5,2,2.5,3,3.5,4);
»y 1 = [4,3.16,2.67,a.34,2.1,1.92,1.78);
»y 2 = [8.83,7.02,’ 5.57,4.43,3.52,2 .. 8,2.22) ;
»sub plat (2,2, 1) r”
»plat (x,y 1,x,y 1, ‘a’ ,’x,y 2,x,y 2, ‘x’) ,x label (‘x’) ,y label (‘y’)
axis([14 0 10))
»sub pi at(2~2,2) .
logy( x f,~,y 1, , a’ , x , y 2, x,y 2 ,.’ x’ ) , x label ( ,x y label (‘y’ )
»sub pl at(2,2,3) . – 1.”
»semi;tag x (x’, y 1, x , y 1, , 0′ , x,y 2, x, y 2, ,x ‘ ) , x label (‘ x ‘ ).,y label (”¥! ).
»sub pat(2,2,4) . . ‘”,J:~” “,:;!; ..”.
»lag lag (x,y 1, x , y 1, , a’ , x,y 2, x,y 2, ,x ‘ ) , x label x )y label (‘y ,I)
a~i s ( [1 .41 10)

Note·that the first data set lies close to a straight line only when plotted-with both scales logarithmic, and the second data set nearly thrms a straight line only on the semilog plot where the vertical axis is logarithmic. In Section 5.5 we explain
how to use these observations to derive a mathematical model for the data .

Frequency-Response Plots arid Filter Circuits
Many electrical applications use specialized circuits called filters to remove signal shaving certain frequencies. Filters work by responding only to signals that have the desired frequencies. These signals are said to “pass through” the circuit. The signals that do not pass through are said to be “filtered out.” For example, a particular circuit in a radio is designed to respond only to signals having the broadcast frequency of the desired radio station. Other circuits, such as those constituting the graphic equalizer, enable the user to select certain musical frequencies such as bass or treble to be passed through to the speakers. The mathematics required to design filter circuits is covered in upper-level engineering courses. However, a simple plot often describes the characteristics of filter circuits. Such a plot, called a frequency-response plot, is often provided when you buy a speaker-amplifier system.

Controlling Tick-Mark Spacing and Labels
The MATLAB set command is a powerful command for.changing the properties of MATLAB “objects,” such as plots. We will not cover this command in depth, but win show how to use it to specify the spacing and labels of the tick marks. To properties that a effect the’ appearance of plot axes are described under the axes

command, which should not be confused with the axis command rN. 00. Up to now we changed the tick-mark spacing by using the axis commarfd’ and hoped that the MATLAB autoscaling feature chose a proper tick-mark spacinge,can are use the following command to specify this spacing. set(gca, ‘XTick’, [Xmin:dx:xmax), ‘Y Tick’, [y min:d y:y max)) Here x min and xmax are the x values that specify the placement of the first
and the las’ttIck I Tiarkson the x~ax’is;-and dx specifies the spacing between tick marks. You would normally use the same values for X minand xmax in both the set and axis commands. Similar’definitions apply to the y-axis values ymin,
ymax, and dy. The term gca ‘stands fo~ “get current axes.” It tells MATLAB
to apply the new values to the axecurrently..used for plotting. For example, to plot y = 0.25×2 for 0 ::: x :::2, with tick marks spaced at intervals of 0.2 on the x-axis and 0.1 on the y-axis, you sould type:

»x = [0:0.01:2);
»y =0.25*x.”2;
»plot (x,y)., set (gca, ‘XTick’, [0: 0.2 :2), ‘YTick’ , [0: 0.1: 1)),
x label (‘x’) ,y label (‘y’)

You can also use the Plot Editor to change the tick spacing. This is discussed in Section 5.4. The set command can also be used to change the tick-mark labels, for example, from numbers to text. Suppose we sell printers, and we want to plot the monthly sales in thousands of dollars from January to June. We can use the set command to label the x-axis with the names of the months, as shown in the following session.The vector x contains the number of the month, and the vector y contains the monthly sales in thousands of dollars

»x = [1: 6) ;
»y = [13,5,7,14,10,12);
»plot (x,y, ‘0’ ,x,y), …
set (gca, ‘XTicklabel’, [‘Jan’;’Feb’;’Mar’;’Apr’ ;’May’;’Jun’)), …
set, (gca, ‘XTick’, [1: 6)) ,axis ([16 0 15)) ,xlabel (‘Month’),
ylabel(‘Monthly Sales ($1000)~), …
title(‘Printer Sales for January to June, ·1997’)

The plot appears in Figure 5.3-6. You can also use the Plot Editor to change labels. Note the labels in the set command must be enclosed in single quotes and are specified as a column vector; thus they are separated by semicolons. Another requirement is that all the labels must have the same number of characters (here, three characters). Table 5.3-2 summarizes the set command.

An example of controlling the tick-mark 'labels with the set command.

An example of controlling the tick-mark ‘labels with the set
command.

The set command specifies properties of objects such as axes. For example, set (gca, ‘XTick’, [xmin:ax:xrnax), ‘YTick’, [ymin:dy:ymax)) specifies the axis limits xmin, xmax, ymin, and ymax and the tick spacing dx and dy. The command
set(gca, ‘XTicklabel’, [‘text’)) specifies the tick labels on the x-axis, where the string text is a column vector that specifies
the tick labels. Each label must be enclosed in single quotes, and all labels must have the same number of characters. For more information, type help axes.

Stem, Stairs, and Bar Plots
MATLAB has several other plot types that are related to xy plots. These include the stem, stairs, and bar plots. Their syntax is very simple; namely, stem (xI Y), ‘
stairs (x , y), and bar (x , y). See Table 5.3-1.

Separate y-Axes
The plot y y function generates a graph with two y-axes. The syntax plot y y (x l , y l, x 2, y 2) plots y l versus x l with y-axis labeling on the left, and plots y 2 versus x 2 with y-axis label ingon the right.The syntax plot y y (xi, y l, x 2,
y 2, ‘type , ‘type 2   ‘) generates a ‘type plot of y l versus x l with y-axis labeling on the left, and generates a ‘type 2’ plot of y 2 versus x 2 with y-axis label ingon the right. For example,plot y y (x l, y l, x 2, y 2, ‘plot’, ‘stem’) uses plot (x l,y l) ‘to generate a plot for the left axis, and stem (x 2 , y 2) to genera\e ‘4’{)\~\\)1\\\t \\~\\\~\~,
Polar Plots
Polar plots are two-dimensional plots made using polar coordinates, If the polar coordinates are (e, r), where e is the angular coordinate and r is the radial coordinate of a point, then the command polar (theta, r ) will produce the polar plot. A grid is automatically overlaid on a polar plot. Thi grid consists of concentric circles and radial lines every 30°. The tit 1 e and g text commands can be used to place a title and text. The variant command polar (theta, r , ‘type’) can be used to specify the line type or data marker, just as with the plot command.

Test Your Understanding

T5.3-1 Obtain the plots shown in Figure 5.3-8. The power function is y = 2 x-O.5,
and the exponential function is y = IOI-x.
T5.3-2 Plot the function y =8 x3 for -1 ::s x :::1 with a tick spacing of 0.25 on
the x-axis and 2 on the y-axis.
T5.3-3 The spiral of Archimedes is described by the polar coordinates (0, r),
where r =aO. Obtain a polar plot of this spiral for 0 ::: 0 :::4rr, with the
parameter a = 2.

The power function y = 2x-O.5 and the exponential function y = IOI-x. .

The power function y = 2x-O.5 and the exponential function
y = IOI-x. .

Posted on July 29, 2015 in advanced Plotting and Model Building

Share the Story

Back to Top
Share This