LESSON 2: 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.
Contents
- DATA FOR THIS LESSON
- SETUP FOR LESSON 2
- EXAMPLE 1: Load the New York City contagious disease data set
- EXAMPLE 2: Define appropriate variables for analysis
- EXAMPLE 3: Plot the measles cases for 1931
- EXAMPLE 4: Plot the measles cases for the month of May
- EXAMPLE 5: Plot the spring measles cases
- EXAMPLE 6: Compare the measles cases for the years 1931 and 1941
- EXAMPLE 7: Adding the rows or columns of an array to summarize data
- EXAMPLE 8: Plot yearly total (in thousands of cases) of measles by year
- EXAMPLE 9*: Try plotting the entire measles array
- EXAMPLE 10*: Attempt to plot all the measles data as a single time series
- EXAMPLE 11*: Correctly plot the measles data as a single time series
- EXAMPLE 12*: Define the x-axis scale for the single time series
- EXAMPLE 13*: Plot the measles data as a single time series, setting x-axis scale
- SUMMARY OF SYNTAX
DATA FOR THIS LESSON
| File | Description |
NYCDiseases.mat |
|
SETUP FOR LESSON 2
- Set the Current Directory to Z:\working\MATLAB\Lesson2. (You will need to make a new directory for Lesson2.)
- Download the data file NYCDiseases.mat to your Lesson2 directory.
- Download the lesson 2 script lesson2Script.m to your Lesson2 directory.
EXAMPLE 1: Load the New York City contagious disease data set
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:
- 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
EXAMPLE 2: Define appropriate variables for analysis
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); % Measles cases in May (column 5 of measles) measlesSpring = measles(:, [3, 4, 5]); % Measles for March, April, May
You should see 4 variables in the Workspace Browser:
- measles1931 - row vector of 12 elements with measles cases for 1931
- measles1941 - row vector of 12 elements with measles cases for 1941
- measlesMay - column vector of 41 elements with May measles for 1931-1971
- measlesSpring - array of 41 rows and 3 columns with spring measles
- Define a variable called mumps1942 that contains the case counts of mumps for the year 1942.
- Define a variable called measlesApril that contains case counts of measles for the month of April.
- Define a variable called chickenPoxSummer that contains the case counts of chicken pox for the summer months (June, July, and August).
EXAMPLE 3: Plot the measles cases for 1931
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('Case count') title('Measles cases NYC: 1931')
You should see a Figure Window with the measles cases for 1931:
Create a new cell right here (beginning of a cell starts with %%). Write MATLAB code to plot the number of measles cases for 1941.
EXAMPLE 4: Plot the measles cases for the month of May
Create a new cell in which you type and execute:
figure % New figure plot(years, measlesMay); % Plot May measles (y-axis) against years (1931 .. 1971) xlabel('Year'); % Label the x-axis ylabel('Case count'); % Label the y-axis title('NYC measles cases for month of May'); % Put a title on the graph
You should see a Figure Window with a single line graph:
Create a new cell right here (beginning of a cell starts with %%). Write MATLAB code to plot the number of mumps cases for April.
EXAMPLE 5: Plot the spring measles cases
Create a new cell in which you type and execute:
figure % New figure plot(years, measlesSpring); % Plot spring measles (y-axis) against years (1931 .. 1971) xlabel('Year'); % Label the x-axis ylabel('Case count'); % Label the y-axis title('NYC measles cases for spring'); % Put a title on the graph legend({'March', 'April', 'May'}) % Use a legend to identify multiple lines
You should see a Figure Window with a single line graph:
Create a new cell right here (beginning of a cell starts with %%). Write MATLAB code to plot the summer chicken pox cases.
EXAMPLE 6: Compare the measles cases for the years 1931 and 1941
Create a new cell in which you type and execute:
figure % New figure hold on % Draw multiple graphs in same figure plot(measles1931, '-sb'); % Draw 1931 measles with blue(b) squares(s) plot(measles1941, '-ok'); % Draw 1941 measles with black(k) circles(o) hold off % No more graphs xlabel('Month') % Label x axis ylabel('Case count') % Label y axis title('Measles cases NYC') % Put a title on the graph legend({'1931', '1941'}) % Use a legend to identify two graphs
You should see a Figure Window with a two line graphs:
EXAMPLE 7: Adding the rows or columns of an array to summarize data
Create a new cell in which you type and execute:
measlesByMonth = sum(measles); % Sum each column of measles measlesByYear = sum(measles, 2); % Sum each row of measles measlesTotal = sum(measles(:)); % Find the total number of measles cases
You should see 3 variables in the Workspace Browser:
- measlesByMonth - row vector of 12 elements with total measles cases for each month
- measlesByYear - column vector of 41 elements with total measles cases for each year
- measlesTotal - a single value with overall total number of measles cases
- Define a variable called mumpsByYear that contains the case counts of mumps for each year.
- Define a variable called measlesAprilTotal that contains the total number of measles cases for the month of April.
- Define a variable called chickenPoxSummerTotal that contains the total number of chicken pox cases for each of the summer months (June, July, and August).
EXAMPLE 8: Plot yearly total (in thousands of cases) of measles by year
Create a new cell in which you type and execute:
figure % Create a new figure window plot(years, measlesByYear./1000); % Draw a line graph xlabel('Year'); % Label the x-axis ylabel('Total cases (in thousands)'); % Label the y-axis title('NYC measles cases'); % Put a title on the graph
You should see a Figure Window with a rescaled y-axis:
EXAMPLE 9*: Try plotting the entire measles array
Create a new cell in which you type and execute:
figure % Create a new figure window plot(measles); % Draw a line graph
You should see a Figure Window with 12 line graphs (the columns of measles):
EXAMPLE 10*: Attempt to plot all the measles data as a single time series
Create a new cell in which you type and execute:
figure % Create a new figure window plot(measles(:)); % Draw a line graph of end-to-end columns
You should see a Figure Window with a single line graph (but it isn't what you want):
EXAMPLE 11*: Correctly plot the measles data as a single time series
Create a new cell in which you type and execute:
measlesFlip = measles'; % Flip measles to make rows into columns figure % Create a new figure window plot(measlesFlip(:)); % Draw a line graph
You should see a Figure Window with a single line graph:
EXAMPLE 12*: Define the x-axis scale for the single time series
Create a new cell in which you type and execute:
yearStart = 1931; % Start of the scale yearInc = 1/12; % Scale has one month intervals yearEnd = 1972 - yearInc; % End of the scale yearScale = yearStart:yearInc:yearEnd; % Yearly scale (with month increments)
You should see yearStart, yearInc, yearEnd, and yearScale variables in the Workspace Browser.
EXAMPLE 13*: Plot the measles data as a single time series, setting x-axis scale
Create a new cell in which you type and execute:
figure % Create a new figure window plot(yearScale, measlesFlip(:)./1000); % Draw a line graph xlabel('Year'); % Label the x-axis ylabel('Cases (in thousands)'); % Label the y-axis title('NYC measles cases'); % Put a title on the graph
You should see a Figure Window with a single line graph:
SUMMARY OF SYNTAX
| MATLAB syntax | Description |
sum(x) |
sums the columns of the array x. |
sum(x, 1) |
sums the columns of the array x. |
sum(x, 2) |
sums the rows of the array x. |
a:b |
forms a row with the values a, a+1,
..., b. |
a:inc:b |
forms a row with the values a, a+inc,
a+2*inc, ..., b. |
title('string') |
puts string as a title above the plot of the current axis. |
xlabel('string') |
uses string as the label for the x-axis of the
current axis. |
ylabel('string') |
uses string as the label for the y-axis of the
current axis. |
legend |
adds an identifying annotation to the current figure. |
A./B |
creates a new array whose elements are the elements of the
array A divided by the
corresponding elements of the array B. If
B consists of a single number, then each
element of A is divided by the value of B. |
x(:) |
creates a single column containing the columns
of the array x positioned end-to-end (i.e.,
the first column of x, followed by
the second column of x, etc.). |
x' |
creates a new array in which the values of
x are flipped along its main diagonal so that
the rows become the columns and the columns become the rows.
x' is called the transpose of x.
|
This lesson was written by Kay A. Robbins of the University of Texas at San Antonio and last modified on 17-Jan-2011. Please contact krobbins@cs.utsa.edu with comments or suggestions. The photo is Child with chickenpox, image 7135384 from <http://www.istockphoto.com>.