Check your understanding of the previous reading: the kinds of plots and what they show, making a plot appear with show, combining plots, importing MatPlotLib, and reading a histogram.

Question 1

Match the type of graph with the kind of information that that type of graph conveys.

#InformationType of graph
1 The relationship between two lists of numbers
2 The distribution of a list of numbers
3 The trend in a list of numbers

Question 2

Write the final function call that should be used after the following code, in order to actually make the plot appear:

import matplotlib.pyplot as plt

plt.hist([1, 7, 5, 4, 3, 5, 6, 7])

Show worked solution

plt.show(). If you do not call the show function, no graph appears. It takes no arguments, but it still needs the parentheses: without them you are not actually calling the function.

Question 3

How many graphs will the following code create?

import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.plot([1,2,3])
plt.show()

Select one answer, then check.

  • Not quite. The code calls show, so a graph does appear.
  • Correct. Both line plots are made before show is called, so they are combined on the same canvas: one graph, with the two identical lines on top of each other.
  • Not quite. Plots made before calling show are combined on the same canvas. You would need a show call after each plot to get two graphs.
  • Not quite. The code makes two line plots, and both come before the single call to show, so they end up on one graph.

Question 4

How many times do you need to import MatPlotLib?

Select one answer, then check.

  • Not quite. MatPlotLib is an external library: a program that makes plots starts with import matplotlib.pyplot as plt.
  • Correct. The import goes at the start of the program; after that, plt is available for every plot the program makes.
  • Not quite. Python runs the program from top to bottom, so plt only exists after the import has run. At the end of the program it comes too late.
  • Not quite. One import is enough: after it, plt stays available for every plot in the program.

Question 5

Bar graphs and Histograms are basically the same thing.

Select one answer, then check.

  • Not quite. The reading warns not to confuse them: bar charts tell you the frequency of categories of data, while histograms group a list of numbers into bins, or ranges of numbers.
  • Correct. Bar charts are a more generalized kind of graph that tell you the frequency of categories of data; histograms take a list of numbers and group them into bins, or ranges of numbers, to show their distribution.

Question 6

Assume that a library exists that can get lists of integers representing students' grades.

from bakery_grades import get_grades
import matplotlib.pyplot as plt

grades = get_grades()

plt.hist(grades)
plt.show()

Executing the code above generates this image:

A histogram titled Student Grades, with grades from 0 to 100 on the x-axis in bins of 10 and the number of students, up to 100, on the y-axis. The bars rise from about 2 students at 0 to about 100 students at 50, then fall to about 5 students at 100.

Which of the following statements is the best interpretation of the graph?

Select one answer, then check.

  • Not quite. The bar at 100 is one of the shortest: only a handful of students.
  • Not quite. The bar at 0 is the shortest of all: only a couple of students.
  • Correct. The bin from 50 to 60 has the tallest bar, about 100 students.
  • Not quite. Evenly distributed grades would give bars of about the same height. Here the bars rise to a peak in the middle and fall off towards both ends.