Python for Introductory StatisticsXYZ Homework Edition

⇩ Download ▾

5.3 Bar Graphs and Pie Charts

Given frequency tables with two columns: categories/values and frequencies:

Syntax for bar graph

import matplotlib.pyplot as plt
value = [x1,x2,...]
freq = [y1,y2,...]
plt.bar(value,freq)

Syntax for pie chart

import matplotlib.pyplot as plt
value = [x1,x2,...]
freq = [y1,y2,...]
plt.pie(freq, labels=value,autopct='%1.1f%%' ) 

Example: 20 students were asked if they were registered to vote. Their responses are given below. Plot a bar graph.

votefreq
Yes11
No9

Watch demo video

  1. Click + Code
  2. Type import matplotlib.pyplot as plt
  3. Type v = ['Yes','No']
  4. Type f = [11,9]
  5. Type plt.bar(v,f)
  6. Click play button
  7. Right click image to save it to your device
import matplotlib.pyplot as plt
v = ['yes','no']
f = [11,9]
plt.bar(v,f)
Show expected output
<BarContainer object of 2 artists>
Bar graph of the voter-registration example: bar yes reaches 11 and bar no reaches 9.

Example: 20 students were asked if they were registered to vote. Their responses are given below. Plot a pie chart (percent).

votefreq
Yes11
No9

Watch demo video

  1. Click + Code
  2. Type import matplotlib.pyplot as plt
  3. Type v = ['Yes','No']
  4. Type f = [11,9]
  5. Type plt.bar(f,labels=f,autopct='%1.1f%%')
  6. Click play button
  7. Right click on the image to save it to your device

Note: autopct='%1.1f%%' changes the frequency to percent rounded to one decimal place, and adds the percent sign %.

import matplotlib.pyplot as plt
v = ['yes','no']
f = [11,9]
plt.pie(f,labels=v,autopct='%1.1f%%')
Show expected output
([<matplotlib.patches.Wedge at 0x7fcbc206d1d0>,
  <matplotlib.patches.Wedge at 0x7fcbc202f3d0>],
 [Text(-0.17207795223283848, 1.086457168210212, 'yes'),
  Text(0.17207795223283862, -1.086457168210212, 'no')],
 [Text(-0.09386070121791189, 0.5926130008419338, '55.0%'),
  Text(0.09386070121791196, -0.5926130008419338, '45.0%')])
Pie chart of the voter-registration example with percentage labels: yes 55.0% and no 45.0%.

Example: 20 students were asked if they were registered to vote. Their responses are given below. Plot a bar graph.

votefreq
Yes11
No9

Add title, vertical and horizontal labels.

  1. Click + Code
  2. Type import matplotlib.pyplot as plt
  3. Type v = ['Yes','No']
  4. Type f = [11,9]
  5. Type plt.bar(v,f)
  6. Type plt.suptitle("Voter Registation")
  7. Type plt.xlabel("Registration")
  8. Type plt.ylabel("Frequency")
  9. Click play button
import matplotlib.pyplot as plt
v = ['yes','no']
f = [11,9]
plt.bar(v,f)
plt.suptitle("Voter Registration")
plt.xlabel("Registration")
plt.ylabel("Frequency")
Show expected output
Text(0, 0.5, 'Frequency')
Bar graph titled Voter Registration with the same yes = 11 and no = 9 bars, now with axis labels Registration and Frequency.

Example: The frequency distribution below is the waiting time, in minutes, of 20 customers at a fast food restaurant.

Waiting_timeFrequency
12
23
34
45
55
61

Plot a bar graph and a pie chart.

Note: First the data values in the two columns are entered separately using Python lists: waiting_time and frequency.

  1. Click + Code
  2. Type import matplotlib.pyplot as plt
  3. Type waiting_time = [1,2,3,4,5,6]
  4. Type frequency = [2,3,4,5,5,1]
  5. Type plt.bar(waiting_time,frequency)
  6. Type plt.xlabel("Waiting Time")
  7. Type plt.ylabel("Frequency")
  8. Click play button
import matplotlib.pyplot as plt

waiting_time = [1,2,3,4,5,6]        
frequency = [2,3,4,5,5,1]      

plt.bar(waiting_time,frequency)        

plt.xlabel("Waiting Time")
plt.ylabel("Frequency")
Show expected output
Text(0, 0.5, 'Frequency')
Bar graph of the fast-food waiting times: frequencies 2, 3, 4, 5, 5 and 1 for waiting times 1 through 6 minutes; axes labeled Waiting Time and Frequency.

Example: The frequency distribution below is the waiting time, in minutes, of 20 customers at a fast food restaurant.

Waiting_timeFrequency
12
23
34
45
55
61

Graph a Pie Chart

import matplotlib.pyplot as plt

waiting_time = [1,2,3,4,5,6]        
frequency = [2,3,4,5,5,1]      
plt.pie(frequency, labels=waiting_time,autopct='%1.1f%%')        
plt.xlabel("Waiting Time")
Show expected output
Text(0.5, 0, 'Waiting Time')
Pie chart of the waiting-time frequencies with percentage labels: times 4 and 5 minutes are the largest slices at 25.0% each, time 6 the smallest at 5.0%.

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or ADA Title II. Every page is built against an automated accessibility gate, and the published editions will meet ADA Title II requirements when they release in late September 2026. If something is unusable, please tell us.