LESSON: Working with line graphs questions
FOCUS QUESTION: How do I display trends in data?
Contents
- 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.
EXAMPLE 1: Load NYC contagious disease data set (load .mat files)
load NYCDiseases.mat;
Questions | Answers |
What kind of files have a .mat extension? | MATLAB MAT-files have a .mat file extension. They are used to store MATLAB variables in an efficient manner. |
How many variables can you read in with a single load? | You can read in all of the variables that are stored in the .mat file. |
Is the format of measles.mat different from that of the count.dat file of Lesson 1? | Yes, count.dat was an ordinary text file that you could read using Word Pad or a browser. MATLAB created a single variable to hold the data of this file based on the name of the file. In contrast, measles.mat was created in MATLAB and holds several variables. You can't open this file using Word Pad or another text editor. |
What if I only want to read particular variables from a MAT-file? | You can read particular variables of the MAT-file by listing them after the file name in the load command. |
EXAMPLE 2: Define variables for analysis (pick out rows and columns)
measles1931 = measles(1, :); measles1941 = measles(11, :); measlesMay = measles(:, 5); measlesSpring = measles(:, [3, 4, 5]);
Questions | Answers |
What happens if I omit the semicolon after the first statement? | MATLAB outputs the values in the first row of measles in the Command Window. Notice that if you take out the semicolon, you will see an orange line underneath the equals sign (=). This orange underline, which is called a warning, designates a potential problem with your script. Red underlines mark actual errors that you must fix. Place your cursor over the underline to see what the problem is and how to fix it. |
What does the equals sign do? | The equals sign (=) is the assignment operator. MATLAB computes a value from the expression on the right and assigns the result to the variable on the left. |
EXAMPLE 3: Plot the measles cases for 1931 (basic plot in new figure)
figure plot(measles1931) xlabel('Month') ylabel('Cases') title('Measles cases NYC: 1931')
EXAMPLE 4: Rescale the measles cases before plotting (basic rescaling)
figure plot(measles1931./1000) xlabel('Month') ylabel('Cases (in thousands)') title('Measles cases NYC: 1931')
Questions | Answers |
What is the difference between A./B and A/B? | A./B is element wise divide, while A/B is matrix right division, which involves finding the inverse of a matrix. We only use element wise division in this course. Note that when B is a single value, the two operations are the same |
EXAMPLE 5: Plot the measles cases for the month of May (give x values explicitly)
figure plot(years, measlesMay./1000) xlabel('Year') ylabel('Cases (in thousands)') title('Measles cases NYC: May (1931-1971)')
Questions | Answers |
Why was it better to use plot(x, y) than plot(y) function for this example? | If you omit the specification of the x values, MATLAB plots against the values 1, 2, ... . In this case, these values should be 1931, 1932, ... . You would need to hand edit the x-axis labels later, a troublesome and error-prone operation. |
EXAMPLE 6: Compare measles cases for 1931 and 1941 (multiple plots same figure)
figure hold on plot(measles1931./1000, '-sb') plot(measles1941./1000, '-ok') hold off xlabel('Month') ylabel('Cases (in thousands)') title('Measles cases NYC') legend('1931', '1941')
Questions | Answers |
What if I omit hold on? | By default, MATLAB replaces one plot by another when you give multiple plot commands. If you didn't call hold on, you would only see the graph corresponding to measles cases in 1941. |
What does hold off do? | The hold off command turns off the MATLAB hold state so that subsequent plots are not added to this figure. |
What does the '-sb' mean in the first plot command? | The 'b' is a short cut for setting the plot line color to blue. The s specifies that the individual data points should be plotted using square markers. The - indicates that successive points should be connected by solid lines, making the graph a line graph. |
What does the 'k' mean in the second plot command? | The 'k' is a short cut for setting the plot line color to black. The o specifies that the individual data points should be plotted using circular markers. The - indicates that successive points should be connected by solid lines. |
Why use different markers on individual graphs plotted on the same axis in addition to plotting the graphs in different colors? | Colors are not always distinguishable when graphs are printed in black and white. Furthermore, people with different forms of color blindness may not be able to distinguish one line from another. |
What happens if I don't set the line colors for the individual plots? | All of the lines will appear in the same color (blue). You could always use the plot tools to edit the graphs later. |
EXAMPLE 7: Plot the spring measles cases (plot multiple columns of an array)
figure plot(years, measlesSpring./1000) xlabel('Year') ylabel('Cases (in thousands)') title('NYC measles cases for spring: 1931-1941') legend('March', 'April', 'May')
EXAMPLE 8: Plot the spring measles and mumps cases in the same figure using subplot.
mumpsSummer = mumps(:, [6,7,8]); 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-1971') % 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-1971') % Put a title on the graph
Questions | Answers |
How is subplot structured? | When you use subplot, you set up an matrix inside the figure that is m by n in size (rows by columns). The third number is the number of current plot you are working on, and is sequenced across the rows, not down. |
These questions were written by Kay A. Robbins of the University of Texas at San Antonio and updated by Dawn Roberson on 4-Jan-2018. Please contact krobbins@cs.utsa.edu with comments or suggestions.