Discrete Data Graphs

Functions for Creating Graphs of Discrete Data

In addition to bar graphs and pie charts, specialized MATLAB® graphics functions appropriately display discrete data. Discrete data generally represents counts of things, such as traffic accidents by month or components produced or rejected during the course of a production run. This section describes how to use stem plots and stairstep plots to display this type of data. The functions for generating discrete data graphs provided are

Function

Description

stem

Displays a discrete sequence of y-data as stems from x-axis

stem3

Displays a discrete sequence of z-data as stems from xy-plane

stairs

Displays a discrete sequence of y-data as steps from x-axis

Two-Dimensional Stem Plots

A stem plot displays data as lines (stems) terminated with a marker symbol at each data value. In a 2-D graph, stems extend from the x-axis.

The stem function displays two-dimensional discrete sequence data. For example, evaluating the function with the values

alpha = .02; beta = .5; t = 0:4:200;
y = exp(-alpha*t).*cos(beta*t);

yields a vector of discrete values for y at given values of t. A line plot shows the data points connected with a straight line.

plot(t,y)

A stem plot of the same function plots only discrete points on the curve.

stem(t,y)

Add axes labels to the x- and y-axis.

xlabel('Time in \musecs')
ylabel('Magnitude')

If you specify only one argument, the number of samples is equal to the length of that argument. In this example, the number of samples is a function of t, which contains 51 elements and determines the length of y.

Customizing the Graph

You can specify the line style, the type of marker, and the color used in the stem plot. For example, adding the string ':sr' specifies a dotted line (:), a square marker (s), and a red color (r). The 'fill' argument colors the face of the marker.

stem(t,y,'--sr','fill')

Setting the aspect ratio of the x- and y-axis to 2:1 improves the utility of the graph. You can do this by setting the aspect ratio of the plot box using pbaspect.

pbaspect([2,1,1])

This is equivalent to setting the PlotBoxApectRatio property directly.

set(gca,'PlotBoxAspectRatio',[2,1,1])

See LineSpec for a list of line styles and marker types.

Combining Stem Plots with Line Plots

Sometimes it is useful to display more than one plot simultaneously with a stem plot to show how you arrived at a result. For example, create a linearly spaced vector with 60 elements and define two functions, a and b.

x = linspace(0,2*pi,60);
a = sin(x);
b = cos(x);

Create a stem plot showing the linear combination of the two functions.

stem_handles = stem(x,a+b);

Overlaying a and b as line plots helps visualize the functions. Before plotting the two curves, set hold to on so the stem plot remains displayed.

hold on
plot_handles = plot(x,a,'--r',x,b,'--g');
hold off

Use legend to annotate the graph. The stem and plot handles passed to legend identify the lines to label. Stem plots are composed of two lines; one draws the markers and the other draws the vertical stems. To create the legend, use the first handle returned by stem, which identifies the marker line.

legend_handles = [stem_handles(1);plot_handles];
legend(legend_handles,'a + b','a = sin(x)','b = cos(x)')

Labeling the axes and creating a title finishes the graph.

xlabel('Time in \musecs')
ylabel('Magnitude')
title('Linear Combination of Two Functions')

Three-Dimensional Stem Plots

stem3 displays 3-D stem plots extending from the xy-plane. With only one vector argument, the stems are plotted in one row at x = 1 or y = 1, depending on whether the argument is a column or row vector. stem3 is intended to display data that you cannot visualize in a 2-D view.

Example – 3-D Stem Plot of an FFT

Fast Fourier transforms are calculated at points around the unit circle on the complex plane. So, it is interesting to visualize the plot around the unit circle. Calculating the unit circle

th = (0:127)/128*2*pi;
x = cos(th);
y = sin(th);

and the magnitude frequency response of a step function

f = abs(fft(ones(10,1),128));

displays the data using a 3-D stem plot, terminating the stems with filled diamond markers.

stem3(x,y,f','d','fill')
view([-65 30])

Label the Graph

Label the graph with the statements

xlabel('Real')
ylabel('Imaginary')
zlabel('Amplitude')
title('Magnitude Frequency Response')

To change the orientation of the view, turn on mouse-based 3-D rotation.

rotate3d on

Example — Combining Stem and Line Plots

Three-dimensional stem plots work well for visualizing discrete functions that do not output a large number of data points. For example, you can use stem3 to visualize the Laplace transform basis function, , for a particular constant value of s.

t = 0:.1:10;	% Time limits
s = 0.1+i;	% Spiral rate
y = exp(-s*t);	% Compute decaying exponential 

Using t as magnitudes that increase with time, create a spiral with increasing height and draw a curve through the tops of the stems to improve definition.

stem3(real(y),imag(y),t)
hold on
plot3(real(y),imag(y),t,'r')
hold off
view(-39.5,62)

Label the Graph

Add axes labels, with the statements

xlabel('Real')
ylabel('Imaginary')
zlabel('Magnitude')

Stairstep Plots

Stairstep plots display data as the leading edges of a constant interval (i.e., zero-order hold state). This type of plot holds the data at a constant y-value for all values between x(i) and x(i+1), where i is the index into the x data. This type of plot is useful for drawing time-history plots of digitally sampled data systems.

Example — Stairstep Plot of a Function

Define a function f that varies over time,

alpha = 0.01;
beta = 0.5;
t = 0:10;
f = exp(-alpha*t).*sin(beta*t);

Use stairs to display the function as a stairstep plot and a linearly interpolated function.

stairs(t,f)
hold on
plot(t,f,'--*')
hold off 

Annotate the graph and set the axes limits.

label = 'Stairstep plot of e^{-(\alpha*t)} sin\beta*t';
text(0.5,-0.2,label,'FontSize',14)
xlabel('t = 0:10','FontSize',14)
axis([0 10 -1.2 1.2])
  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS