Visualization Exercises Part 7

This is the seventh and last of several exercise pages on visualization using Matplotlib that were prepared for use in the course ITSE 1302 at Austin Community College.

The remainder of this exercise page will concentrate on bar charts and pie charts.

You will find other examples in the matplotlib gallery, which provides dozens of different data visualization examples along with the code required to produce those visualizations.

Import required libraries

In [1]:
import numpy as np
import matplotlib.pyplot as plt

A simple bar chart

Ben Alex Keen tells us that bar charts "are used to display values associated with categorical data." Someone at Yale University tells us that "Categorical variables represent types of data which may be divided into groups. Examples of categorical variables are race, sex, age group, and educational level."

Bar charts are also often used to provide visual comparisons of such values.

Assume we want to compare the heights in inches of the members of a basketball team. We can do that with the following code that uses the Axes.bar method to create a bar chart showing the height of each player identified by the player's name. Note that the horizontal arrangement of the bars in a bar chart is often not critical. In this case, we will arrange the bars alphabetically by the player's name.

In [2]:
names = ['Alex','Bob','Don','Frank','Harry']
xPosition = range(1,6)
heights = [78,62,72,60,75]

fig,ax = plt.subplots(1,1)
ax.bar(xPosition,heights,tick_label=names)

ax.grid()
plt.show()

Cosmetic improvement

The following code adds some cosmetic improvements to the bar chart by adding labels to the axes and adding a title.

In [3]:
names = ['Alex','Bob','Don','Frank','Harry']
xPosition = range(1,6)
heights = [78,62,72,60,75]

fig,ax = plt.subplots(1,1)
ax.bar(xPosition,heights,tick_label=names)
ax.set_xlabel('Player Name')
ax.set_ylabel('Player Height in Inches')
ax.set_title('Player Height Comparison')

ax.grid()
plt.show()

A horizontal bar chart

The following code displays a horizontal bar chart for the same data.

In [4]:
names = ['Alex','Bob','Don','Frank','Harry']
xPosition = range(1,6)
heights = [78,62,72,60,75]

fig,ax = plt.subplots(1,1)
ax.barh(xPosition,heights,tick_label=names)

ax.set_ylabel('Player Name')
ax.set_xlabel('Player Height in Inches')
ax.set_title('Player Height Comparison')
ax.grid()
plt.show()

Bar chart with multiple x-values

Suppose we want to compare the individual scores achieved by each of our five basketball players in three consecutive games. We can accomplish that with the following code, which shows the three scores for each player in a side-by-side bar chart display. This chart also includes a legend that links the colors to the game number.

In [5]:
names = ['Alex','Bob','Don','Frank','Harry']

xPosition = np.arange(5)
game01 = [78,62,72,60,75]
game02 = [34,70,28,54,36]
game03 = [48,50,60,46,62]

width = 0.25

fig,ax = plt.subplots(1,1)
ax.bar(xPosition,game01,width,label="Game 1")
ax.bar(xPosition+width,game02,width,label="Game 2",tick_label=names)
ax.bar(xPosition+width*2,game03,width,label="Game 3")
ax.set_xlabel('Player Name')
ax.set_ylabel('Player Scores')
ax.set_title('Player Score Comparison')

ax.grid()
plt.legend(loc='lower left')
plt.show()

A stacked bar chart

Another way to compare the individual scores achieved by each of our five players in three consecutive games is to stack the scores. We can accomplish that with the following code.

In [6]:
names = ['Alex','Bob','Don','Frank','Harry']

xPosition = np.arange(5)
game01 = np.array([78,62,72,60,75])
game02 = np.array([34,70,28,54,36])
game03 = np.array([48,50,60,46,62])

fig,ax = plt.subplots(1,1)
ax.bar(xPosition,game01,bottom=0,label="Game 1",tick_label=names)
ax.bar(xPosition,game02,bottom=game01,label="Game 2")
ax.bar(xPosition,game03,bottom=(game01+game02),label="Game 3")
ax.set_xlabel('Player Name')
ax.set_ylabel('Player Scores')
ax.set_title('Player Score Comparison')

ax.grid()
plt.legend(loc='lower left')
plt.show()

A basic pie chart

Wikipedia describes a pie chart as follows:

"A Pie chart (or a circle chart) is a circular statistical graphic which is divided into slices to illustrate numerical proportion. In a pie chart, the arc length of each slice (and consequently its central angle and area), is proportional to the quantity it represents. While it is named for its resemblance to a pie which has been sliced, there are variations on the way it can be presented. The earliest known pie chart is generally credited to William Playfair's Statistical Breviary of 1801.

Pie charts are very widely used in the business world and the mass media. However, they have been criticized and many experts recommend avoiding them, pointing out that research has shown it is difficult to compare different sections of a given pie chart, or to compare data across different pie charts. Pie charts can be replaced in most cases by other plots such as the bar chart, box plot or dot plots."

If you examine the pie chart documentation you will see that the Axes.pie method takes many parameters that can be used to customize the appearance of the chart.

Continuing with our basketball theme, we will create a pie chart that can be used to visually compare the number of points scored by the player named Alex in seven consecutive games.

In [7]:
games = 'Game 1', 'Game 2', 'Game 3', 'Game 4', 'Game 5','Game 6','Game 7'
points = [78, 34, 48, 20, 62, 37, 29]# points scored during each game
explodeSlices = (0.2, 0, 0, 0.3, 0, 0, 0)#explode largest and smallest
percentages = '%1.1f%%' #show percentage of each slice

fig1, ax = plt.subplots()
ax.pie(points, explode=explodeSlices, labels=games, autopct=percentages,
        shadow=True, startangle=90)
ax.axis('equal')  # ensure that pie is drawn as a circle.
ax.set_title('Comparison of scores by game for Alex')
plt.show()

Summary

This is the final exercise page on visualizations. However, we have barely scatched the surface in terms of what can be done with visualizations using matplotlib. It is strongly recommended that you visit the matplotlib gallery and work through the code for most, if not all of the examples provided there.

Housekeeping material

Author: Prof. Richard G. Baldwin, Austin Community College, Austin, TX

File: VisualizationPart07.ipynb

Revised: 04/22/18

Copyright 2018 Richard G. Baldwin

-end-