Besides lines, data can be shown as the shape of its distribution or as shares of a whole. Histograms and pie charts do these two jobs.

Histogram

A histogram shows how values are spread over intervals called bins. The height of each bar gives the number of values in that interval, and the bars touch because the intervals are continuous. It is drawn with plt.hist(data, bins=n). It is used to see whether data are symmetric, skewed or have more than one peak. Unlike a bar chart, which compares separate categories, a histogram shows one numerical variable.

Definition: Histogram. A graph of bars whose heights show how many values of a numerical variable fall in each interval.

Pie Chart

A pie chart shows how a whole is divided among categories. Each slice is proportional to its share, so the angle of the slice is its fraction of 360 degrees. It is drawn with plt.pie(values, labels=names, autopct="%1.1f%%"), where autopct writes the percentage on each slice. It is best for a few categories that add up to a whole, such as the share of marks in different subjects. Too many slices make it hard to read.

import matplotlib.pyplot as plt
marks = [12, 15, 18, 20, 22, 22, 25, 25, 25, 27, 28, 30]
plt.hist(marks, bins=4)
plt.show()
share = [40, 30, 20, 10]
plt.pie(share, labels=["A", "B", "C", "D"], autopct="%1.1f%%")
plt.show()

Matplotlib also offers a bar chart through plt.bar(names, values), a scatter plot through plt.scatter(x, y), and several graphs in one figure through plt.subplot(rows, columns, position). Choosing the right kind of graph is part of good data presentation. A line suits change over time, a bar chart compares categories, a scatter plot shows the relation between two variables, a histogram shows spread, and a pie chart shows shares. Every graph should have a title, labelled axes with units, and a legend when it has more than one series.

Table 4.4: Histogram and pie chart compared

Feature Histogram Pie chart
Shows Spread of values Share of a whole
Data One numerical variable Categories adding to a total
Function plt.hist() plt.pie()
Bars or slices Touching bars Slices of a circle

Example 4.4: Counting the values in each bin

Problem: For the 20 marks in the figure, find how many fall in the bins 10-20, 20-30, 30-40 and 40-50.

Solution:

  1. Use np.histogram with the bin edges 10, 20, 30, 40, 50.
  2. Read the counts from the result.

Result: The counts are 3, 8, 6 and 3, which are the heights of the four bars.

import numpy as np
marks = [12, 15, 18, 20, 22, 22, 25, 25, 25, 27, 28, 30, 30,
         32, 34, 35, 38, 40, 42, 45]
counts, edges = np.histogram(marks, bins=[10, 20, 30, 40, 50])
print(counts, edges)
print(sum([40, 30, 20, 10]))

Output

[3 8 6 3] [10 20 30 40 50]
100

Common mistakes

  • Using a pie chart for categories that do not make up a whole.
  • Choosing too few or too many bins, which hides or exaggerates the pattern.
  • Confusing a histogram with a bar chart of categories.

Key takeaways

  • plt.hist draws a histogram, and bins set the intervals.
  • plt.pie draws a pie chart, and autopct shows percentages.
  • A histogram shows the spread of one numerical variable.
  • A pie chart shows shares of a whole.

Practice questions

  1. What does a histogram show?
  2. Which argument of plt.pie writes the percentage on each slice?
  3. How does a histogram differ from a bar chart?

Answers to the practice questions

  1. How values of a numerical variable are spread over intervals.
  2. autopct.
  3. A histogram shows one numerical variable in continuous intervals, while a bar chart compares separate categories.

Sources

  • Official BCA syllabus Python Programming, course code 26BCA403SEC01, Unit IV