LESSON: Working with line graphs

FOCUS QUESTION: How do I display trends in data?

This lesson shows you different ways to plot the rows and columns of a table or array using line graphs. It introduces ways to display multiple plots at the same time.

In this lesson you will:

  • Read multiple variables from a .mat file.
  • Plot arrays using line graphs.
  • Use the colon operator (:) to extract rows and columns.
  • Use subplot to print multiple plots in the same figure.
Young child with chicken pox

Contents

DATA FOR THIS LESSON

File Description
NYCDiseases.mat The data set contains the monthly totals of the number of new cases of measles, mumps, and chicken pox for New York City during the years 1931-1971.

The file is organized into the following variables:

  • measles - an array containing the monthly cases of measles
  • mumps - an array containing the monthly cases of mumps
  • chickenPox - an array containing the monthly cases of chicken pox
  • years - a vector containing the years 1931 through 1971
The data was extracted from the Hipel-McLeod Time Series Datasets Collection, available at http://www.stats.uwo.ca/faculty/aim/epubs/mhsets/readme-mhsets.html.

The data was first published in: Yorke, J.A. and London, W.P. (1973). "Recurrent Outbreaks of Measles, Chickenpox and Mumps", American Journal of Epidemiology, Vol. 98, pp. 469.

SETUP FOR THE LINE GRAPHS LESSON

EXAMPLE 1: Load NYC contagious disease data set (load .mat files)

Create a new cell in which you type and execute:

   load NYCDiseases.mat;    % Load the NYC disease data

You should see 4 variables in the Workspace Browser:

EXERCISE 1: Diagramming an array
It is important that you understand how the data is structured. Write several sentences describing the data structure (what is in rows and what are in the columns). This is called a 'word picture'. Draw a word picture of the measles array and label its rows and columns. Put a % sign in front of your sentences (they should turn green), and MATLAB will ignore them.

EXAMPLE 2: Define variables for analysis (pick out rows and columns)

Create a new cell in which you type and execute:

   measles1931 = measles(1, :);  % Measles cases in 1931(row 1 of measles)
   measles1941 = measles(11, :); % Measles cases in 1941(row 11 of measles)
   measlesMay = measles(:, 5);   % May measles cases (column 5 of measles)
   measlesSpring = measles(:, [3, 4, 5]); % Measles for March, April, May

You should see 4 variables in the Workspace Browser:

EXERCISE 2: Create variables for various rows and columns

EXAMPLE 3: Plot the measles cases for 1931 (basic plot in new figure)

Create a new cell in which you type and execute:

   figure                       % Create a new figure window
   plot(measles1931)            % Plot 1931 measles (y-axis) against 1..12
   xlabel('Month')              % Always label your axes
   ylabel('Cases')
   title('Measles cases NYC: 1931')

You should see a Figure Window with the measles cases for 1931:

EXERCISE 3: Plot measles cases for 1941 in a new figure.

EXAMPLE 4: Rescale the measles cases before plotting (basic rescaling)

Create a new cell in which you type and execute:

   figure                       % Create a new figure window
   plot(measles1931./1000)      % Plot 1931 measles (y-axis) against 1..12
   xlabel('Month')              % Always label your axes
   ylabel('Cases (in thousands)')
   title('Measles cases NYC: 1931')

You should see a Figure Window with the measles cases for 1931:

EXERCISE 4: Plot measles cases for 1941, rescaling y.

EXAMPLE 5: Plot the measles cases for the month of May (give x values explicitly)

Create a new cell in which you type and execute:

   figure                                      % New figure
   plot(years, measlesMay./1000)   % Plot May measles (y-axis) vs years
   xlabel('Year')                              % Label the x-axis
   ylabel('Cases (in thousands)')              % Label the y-axis
   title('Measles cases NYC: May (1931-1971)') % Put a title on the graph

You should see a Figure Window with a single line graph:

EXERCISE 5: Plot April chicken pox cases, giving x values explicitly.

EXAMPLE 6: Compare measles cases for 1931 and 1941 (multiple plots same figure)

Create a new cell in which you type and execute:

   figure                         % New figure
   hold on                        % Draw multiple graphs in same figure
   plot(measles1931./1000, '-sb') % Show 1931 with blue(b) squares(s)
   plot(measles1941./1000, '-ok') % Show 1941 with black(k) circles(o)
   hold off                       % No more graphs
   xlabel('Month')                % Label x axis
   ylabel('Cases (in thousands)') % Label y axis
   title('Measles cases NYC')     % Put a title on the figure
   legend('1931', '1941')         % Use a legend to identify two graphs

You should see a Figure Window with a two line graphs:

EXERCISE 6: Plot 1941 measles and 1942 chicken pox cases on same graph.
Show measles in red and chicken pox in black.

EXAMPLE 7: Plot the spring measles cases (plot multiple columns of an array)

Create a new cell in which you type and execute:

   figure                                 % New figure
   plot(years, measlesSpring./1000)   % Plot spring measles (y-axis) against years (1931 .. 1971)
   xlabel('Year')                         % Label the x-axis
   ylabel('Cases (in thousands)')         % Label the y-axis
   title('NYC measles cases for spring: 1931-1941')  % Put a title on the graph
   legend('March', 'April', 'May')        % Legend identifies multiple lines

You should see a Figure Window with a single line graph:

EXERCISE 7: Plot summer mumps cases on one graph.

EXAMPLE 8: Plot the spring measles and mumps cases in the same figure using subplot.

Create a new cell in which you type and execute:

   figure                                 % New figure
   subplot(1,2,1)                     % The figure will have 1 x 2 figures - this is the first
   plot(years, measlesSpring./1000)   % Plot spring measles (y-axis) against years (1931 .. 1971)
   xlabel('Year')                         % Label the x-axis
   ylabel('Cases (in thousands)')         % Label the y-axis
   legend('March', 'April', 'May')        % Legend identifies multiple lines
   title('NYC measles cases spring: 1931-1941')  % Put a title on the graph
   subplot(1,2,2)                     % The figure will have 1 x 2 figures - this is the 2nd
   plot(years, mumpsSummer./1000)     % Plot summer mumps (y-axis) against years
   xlabel('Year')
   ylabel('Cases (in thousands)')
   legend('June','July','August')
   title('NYC mumps cases summer: 1931-1941')  % Put a title on the graph

You should see a Figure Window with 2 line graphs, side by side:

EXERCISE 8: Look at the 2 graphs you just generated. Write a short paragraph comparing them. Can they be directly compared? Why or why not? What would you tell potential tourists about visiting NYC in the 1930s and 1940s? When would you tell them to visit (spring or summer) and why? What happened over the course of the graph (1940s to 1970s) that made the numbers go way down?

EXERCISE 9: Plot measles from 1931 and from 1941 in one figure, two graphs, side-by-side.

SUMMARY OF SYNTAX

MATLAB syntax Description
A(n, :) Pick row number n out of the array A. The : designates all columns.
A(:, n) Pick column number n out of the array A. The : designates all rows.
[a, b, c] Form a new array with the arrays a, b, and c placed side-by-side.
[a; b; c] Form a new array with the arrays a, b, and c placed vertically end-to-end.
A./B Create a new array whose elements are the elements of the array A divided by the corresponding elements of the array B. If B is a single number, then each element of A is divided by the value of B.
Plot colors 'r' (Red), 'g' (Green), 'b' (Blue), 'c' (Cyan), 'm' (Magenta), 'y' (Yellow), 'k' (Black), 'w' (White)
Plot shapes '+' (Plus sign), 'o' (Circle), '*' (Asterisk), '.' (Point), 'x' (Cross), 's' (Square'), 'd' (Diamond), '^' (Upward-pointing triangle), 'v' (Downward-pointing triangle), '>' (Right-pointing triangle), '<' (Left-pointing triangle), 'p' (pentagram), 'h' (hexagram)
Plot lines '-' (Solid line), '--' (Dashed line), ':' (Dotted line), '-.'( Dash-dot line)
subplot(m,n,p) Plot multiple graphs in same figure. Divides the current figure into an m-by-n grid. p is the # of the plot, where the first figure is the first row, first column, the second is the second column of the first row, and so on

This lesson was written by Kay A. Robbins of the University of Texas at San Antonio and last modified by Dawn Roberson on 4-Jan-2018. Please contact krobbins@cs.utsa.edu with comments or suggestions. The photo is Child with chickenpox, image 7135384 from <http://www.istockphoto.com>.