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.
Contents
- DATA FOR THIS LESSON
- SETUP FOR THE LINE GRAPHS LESSON
- EXAMPLE 1: Load NYC contagious disease data set (load .mat files)
- EXAMPLE 2: Define variables for analysis (pick out rows and columns)
- EXAMPLE 3: Plot the measles cases for 1931 (basic plot in new figure)
- EXAMPLE 4: Rescale the measles cases before plotting (basic rescaling)
- EXAMPLE 5: Plot the measles cases for the month of May (give x values explicitly)
- EXAMPLE 6: Compare measles cases for 1931 and 1941 (multiple plots same figure)
- EXAMPLE 7: Plot the spring measles cases (plot multiple columns of an array)
- EXAMPLE 8: Plot the spring measles and mumps cases in the same figure using subplot.
- SUMMARY OF SYNTAX
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:
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
- Create a LineGraphs directory on your V: drive and make it your current directory.
- Download the NYCDiseases.mat to your LineGraphs directory.
- Create a LineGraphsLesson script file in your LineGraphs directory.
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:
- 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
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:
- 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 chickenPox1942 that contains the case counts of chicken pox for the year 1942.
- Define a variable called measlesApril that contains case counts of measles for the month of April.
- Define a variable called mumpsSummer that contains the case counts of mumps for the summer months (June, July, and August).
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:
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:
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:
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:
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:
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:
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>.