LESSON 3: Pie and bar charts
FOCUS QUESTION: How can I show proportions and relative sizes of different data groups?
This lesson takes you through different ways of visualizing data using pie charts and bar charts.
Contents
- DATA FOR THIS LESSON
- SETUP FOR LESSON 3
- EXAMPLE 1: Load the data about New York contagious diseases
- EXAMPLE 2: Compute yearly totals of the three diseases
- EXAMPLE 3: Show a pie chart of the yearly totals of the three diseases
- EXAMPLE 4: Make a bar chart of the disease totals
- EXAMPLE 5: Calculate individual and overall monthly totals
- EXAMPLE 6: Make a bar chart of the measles monthly case totals (thousands)
- EXAMPLE 7: Make a side-by-side bar chart of monthly totals for the 3 diseases
- EXAMPLE 8: Make a stacked bar chart of monthly totals for the 3 diseases
- EXAMPLE 9: Make a horizontal bar chart of monthly totals for the 3 diseases
- EXAMPLE 10: Make a stacked horizontal bar chart using a different color scheme
- SUMMARY OF SYNTAX
DATA FOR THIS LESSON
| File | Description |
NYCDiseases.mat |
|
SETUP FOR LESSON 3
- Set the Current Directory to Z:\working\MATLAB\Lesson3. (You will need to make a new directory for Lesson3.)
- Download the data file NYCDiseases.mat to your Lesson2 directory.
- Download the lesson 3 script lesson3Script.m to your Lesson3 directory.
EXAMPLE 1: Load the data about New York contagious diseases
Create a new cell in which you type and execute:
load NYCDiseases.mat; % Load the disease data
You should see 4 variables in the Workspace Browser:
- measles - an array containing the monthly totals of measles
- mumps - an array containing the monthly totals of mumps
- chickenPox - an array containing the monthly totals of chicken pox
- years - a vector containing the years 1931 through 1971
EXAMPLE 2: Compute yearly totals of the three diseases
Create a new cell in which you type and execute:
totalMeasles = sum(measles(:)); % Find the total number of measles cases totalMumps = sum(mumps(:)); % Find the total number of mumps cases totalCP = sum(chickenPox(:)); % Find the total number of chicken pox cases diseaseTotals = [totalMeasles, totalMumps, totalCP]; % Make a totals vector
You should see new 4 variables in the Workspace Browser:
- totalMeasles - the overall total number of measles cases
- totalMumps - the overall total number of mumps cases
- totalCP - the overall total number of chicken pox cases
- diseaseTotals - combines the three totals into a single row vector.
- Draw and label a picture of the
diseaseTotalsvector. - Write a MATLAB statement to define a
totalCasesvariable containing the total number of cases of all three diseases.
EXAMPLE 3: Show a pie chart of the yearly totals of the three diseases
Create a new cell in which you type and execute:
figure % Create a new figure window pie(diseaseTotals, {'Measles', 'Mumps', 'Chicken pox'}) % Draw a pie chart title('Contagious childhood diseases in New York City 1931-1971');
You should see a Figure Window with a pie chart of the three diseases:
- There are 1,305,300 total cases of disease and 697,948 of them are measles. Approximately what percentage of the total cases are measles? How did you get your answer?
- Write a MATLAB statement to define a variable
measlesPercentthat holds the percentage of total disease cases that were measles. Assume that you have already defined a variable calledtotalCasescontaining the overall total number of cases and a variable calledtotalMeaslescontaining the total number of measles cases (from EXAMPLE 2).
Create a new cell right here (beginning of a cell starts with %%). Define 3 variables for the total number of cases of measles, mumps, and chicken pox, respectively for the year 1941. Write MATLAB code to create a pie chart of the number of measles, mumps, and chicken pox cases for 1941. Compare the results with the pie chart of EXAMPLE 3.
EXAMPLE 4: Make a bar chart of the disease totals
Create a new cell in which you type and execute:
figure % Create a new figure bar(diseaseTotals) % Plot a bar chart in it set(gca, ... % Label the tick marks rather than x-axis 'XTickLabelMode', 'manual', ... % Doing tick marks by hand 'XTickLabel', {'Measles', 'Mumps', 'Chicken pox'}) ylabel('Cases'); % Label the y-axis title('Contagious childhood diseases in NYC: 1931-1971');
You should see a Figure Window containing a labeled bar chart bar chart:
Create a new cell right here (beginning of a cell starts with %%). Write MATLAB code to create a bar graph of the number of measles cases for 1941.
EXAMPLE 5: Calculate individual and overall monthly totals
Create a new cell in which you type and execute:
measlesByMonth = sum(measles); % Find the monthly totals of measles mumpsByMonth = sum(mumps); % Find the monthly totals of mumps CPByMonth = sum(chickenPox); % Find the monthly totals of chicken pox byMonth = [measlesByMonth', mumpsByMonth', CPByMonth']; %Make 3 columns of totals
You should see 4 variables in the Workspace Browser:
- measlesByMonth - row vector of 12 elements containing monthly measles totals
- mumpsByMonth - row vector of 12 elements containing the monthly mumps totals
- CPByMonth - row vector of 12 elements containing the monthly chicken
- byMonth - an array with 12 rows and 3 columns holding monthly totals for the three diseases.
EXAMPLE 6: Make a bar chart of the measles monthly case totals (thousands)
Create a new cell in which you type and execute:
figure % Create a new figure bar(measlesByMonth./1000) % Plot a bar chart of measles monthly totals xlabel('Month') % Label the x-axis ylabel('Cases (in thousands)') % Label the y-axis title('Measles by month in NYC: 1931-1971'); % Put a title on the graph
You should see a Figure Window containing a labeled bar chart:
Add a cell in which you create a new figure similar to that created by EXAMPLE 6. Plot mumps instead of measles.
EXAMPLE 7: Make a side-by-side bar chart of monthly totals for the 3 diseases
Create a new cell in which you type and execute:
figure % Create a new figure bar(byMonth./1000) % Plot a bar chart of monthly totals xlabel('Month') % Label the x-axis ylabel('Cases (in thousands)') % Label the y-axis title('Childhood diseases by month in NYC: 1931-1971'); % Put a title on the graph legend('Measles', 'Mumps', 'Chicken pox')
You should see a Figure Window containing a labeled bar chart:
EXAMPLE 8: Make a stacked bar chart of monthly totals for the 3 diseases
Create a new cell in which you type and execute:
figure % Create a new figure bar(byMonth./1000, 'stack') % Plot a stacked bar chart of monthly totals xlabel('Month') % Label the x-axis ylabel('Cases (in thousands)') % Label the y-axis title('Childhood diseases by month in NYC: 1931-1971'); % Put a title on the graph legend('Measles', 'Mumps', 'Chicken pox') % Need a legend
You should see a Figure Window containing a labeled stacked bar chart:
EXAMPLE 9: Make a horizontal bar chart of monthly totals for the 3 diseases
Create a new cell in which you type and execute:
figure % Create a new figure barh(byMonth./1000) % Plot a stacked bar chart of monthly totals xlabel('Cases (in thousands)') % Label the x-axis ylabel('Month') % Label the y-axis title('Childhood diseases by month in NYC: 1931-1971'); % Put a title on the graph legend('Measles', 'Mumps', 'Chicken pox') % Need a legend
You should see a Figure Window containing a labeled horizontal stack bar chart:
EXAMPLE 10: Make a stacked horizontal bar chart using a different color scheme
Create a new cell in which you type and execute:
figure % Create a new figure colormap summer % Use a new color scheme barh(byMonth./1000, 'stack') % Plot a stacked bar chart of monthly totals xlabel('Cases (in thousands)') % Label the x-axis ylabel('Month') % Label the y-axis title('Childhood diseases by month in NYC: 1931-1971'); % Put a title on the graph legend('Measles', 'Mumps', 'Chicken pox') % Need a legend
You should see a Figure Window containing a labeled horizontal stacked bar chart that uses a summer color scheme:
SUMMARY OF SYNTAX
| MATLAB syntax | Description |
[a, b, c] |
forms an array with values of a, b
and c placed side-by-side. |
[a; b; c] |
forms an array with values of a, b
and c placed vertically end-to-end. |
pie(y) |
draws a pie chart for y. Each element
of y corresponds to a slice of the pie in proportion
to the element's fraction of the total. MATLAB draws a
partial pie if the sum of the elements of y is
less than 1. |
bar(y) |
draws a bar for each element of y against
the values 1, 2, ... . If
y is a 2D array, MATLAB groups the bars by
row. |
bar(x, y) |
draws a bar for each element of y against
the values of x. If
y is a 2D array, MATLAB groups the bars by
row. |
barh(y) |
is the same as bar, except that MATLAB draws
horizontal bars rather than vertical bars. (Beware that
you will need to reverse the labels for the x and y axes.) |
bar(y, 'stack') |
draws a bar chart in which the height of a bar is the sum
of the corresponding row of y. |
colormap aMap |
sets the list of colors to use (the current
color map) to those listed in the variable aMap.
MATLAB has several built-in lists including summer and
jet. Alternatively, you can create your own list of colors. |
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 NYC View, a photo of the Empire State Building taken from a roof at Lexington Ave and 37th, image 1857382 from <http://www.istockphoto.com>.