Exploratory Data Analysis -- Visual Display of Data

This lesson will introduce the matplotlib external module package, and examine how to construct line charts, scatter plots, bar charts, box plot, and histograms using methods in matplotlib and pandas

The theory of histograms will appear in later lessons, here we only show how to construct one using matplotlib

About matplotlib

Quoting from: https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py

matplotlib.pyplot is a collection of functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.

In matplotlib.pyplot various states are preserved across function calls, so that it keeps track of things like the current figure and plotting area, and the plotting functions are directed to the current axes (please note that "axes" here and in most places in the documentation refers to the axes part of a figure and not the strict mathematical term for more than one axis).

Background

Data are not always numerical. Data can music (audio files), or places on a map (georeferenced attributes files), images (various imge files, e.g. .png, jpeg)

They can also be categorical into which you can place individuals:

  • The individuals are cartons of ice-cream, and the category is the flavor in the carton
  • The individuals are professional basketball players, and the category is the player's team.

Bar Graphs

Bar charts (graphs) are good display tools to graphically represent categorical information. The bars are evenly spaced and of constant width. The height/length of each bar is proportional to the relative frequency of the corresponding category.

Relative frequency is the ratio of how many things in the category to how many things in the whole collection.

The example below uses matplotlib to create a box plot for the ice cream analogy, the example is adapted from an example at https://www.geeksforgeeks.org/bar-plot-in-matplotlib/

In [1]:
ice_cream = {'Chocolate':16, 'Strawberry':5, 'Vanilla':9} # build a data model 
import matplotlib.pyplot # the python plotting library

flavors = list(ice_cream.keys()) # make a list object based on flavors
cartons = list(ice_cream.values()) # make a list object based on carton count -- assumes 1:1 association!

myfigure = matplotlib.pyplot.figure(figsize = (10,5)) # generate a object from the figure class, set aspect ratio

# Built the plot
matplotlib.pyplot.bar(flavors, cartons, color ='maroon', width = 0.4) 
matplotlib.pyplot.xlabel("Flavors") 
matplotlib.pyplot.ylabel("No. of Cartons in Stock") 
matplotlib.pyplot.title("Current Ice Cream in Storage") 
matplotlib.pyplot.show() 

Lets tidy up the script so it is more understandable, a small change in the import statement makes a simpler to read (for humans) script - also changed the bar colors just 'cause!

In [2]:
ice_cream = {'Chocolate':16, 'Strawberry':5, 'Vanilla':9} # build a data model 
import matplotlib.pyplot as plt # the python plotting library

flavors = list(ice_cream.keys()) # make a list object based on flavors
cartons = list(ice_cream.values()) # make a list object based on carton count -- assumes 1:1 association!

myfigure = plt.figure(figsize = (10,5)) # generate a object from the figure class, set aspect ratio

# Built the plot
plt.bar(flavors, cartons, color ='orange', width = 0.4) 
plt.xlabel("Flavors") 
plt.ylabel("No. of Cartons in Stock") 
plt.title("Current Ice Cream in Storage") 
plt.show() 

Now lets deconstruct the script a bit:

ice_cream = {'Chocolate':16, 'Strawberry':5, 'Vanilla':9} # build a data model 
import matplotlib.pyplot as plt # the python plotting library

flavors = list(ice_cream.keys()) # make a list object based on flavors
cartons = list(ice_cream.values()) # make a list object based on carton count -- assumes 1:1 association!

This part of the code creates a dictionary object, keys are the flavors, values are the carton counts (not the best way, but good for our learning needs). Next we import the python plotting library from matplotlib and name it plt to keep the script a bit easier to read.

Next we use the list method to create two lists from the dictionary, flavors and cartons. Keep this in mind plotting is usually done on lists, so we need to prepare the structures properly.

The next statement

myfigure = plt.figure(figsize = (10,5)) # generate a object from the figure class, set aspect ratio

Uses the figure class in pyplot from matplotlib to make a figure object named myfigure, the plot is built into this object. Every call to a method in plt adds content to myfigure until we send the instruction to render the plot (plt.show())

The next portion of the script builds the plot:

plt.bar(flavors, cartons, color ='orange', width = 0.4) # Build a bar chart, plot series flavor on x-axis, plot series carton on y-axis.  Make the bars orange, set bar width (units unspecified)
plt.xlabel("Flavors") # Label the x-axis as Flavors
plt.ylabel("No. of Cartons in Stock") # Label the x-axis as Flavors
plt.title("Current Ice Cream in Storage") # Title for the whole plot

This last statement renders the plot to the graphics device (probably localhost in the web browser)

plt.show() 

Now lets add another set of categories to the plot and see what happens

In [3]:
ice_cream = {'Chocolate':16, 'Strawberry':5, 'Vanilla':9} # build a data model 
eaters = {'Cats':6, 'Dogs':5, 'Ferrets':19} # build a data model 
import matplotlib.pyplot as plt # the python plotting library

flavors = list(ice_cream.keys()) # make a list object based on flavors
cartons = list(ice_cream.values()) # make a list object based on carton count -- assumes 1:1 association!

animals = list(eaters.keys()) 
beasts = list(eaters.values()) 
myfigure = plt.figure(figsize = (10,5)) # generate a object from the figure class, set aspect ratio

# Built the plot
plt.bar(flavors, cartons, color ='orange', width = 0.4) 
plt.bar(animals, beasts, color ='green', width = 0.4) 
plt.xlabel("Flavors") 
plt.ylabel("Counts: Cartons and Beasts") 
plt.title("Current Ice Cream in Storage") 
plt.show() 

Now suppose we want horizontal bars we can search pyplot for such a thing. If one types horizontal bar chart into the pyplot search engine there is a link that leads to:

Which has the right look! If we examine the script there is a method called barh so lets try that.

In [4]:
ice_cream = {'Chocolate':16, 'Strawberry':5, 'Vanilla':9} # build a data model 
eaters = {'Cats':6, 'Dogs':5, 'Ferrets':19} # build a data model 
import matplotlib.pyplot as plt # the python plotting library

flavors = list(ice_cream.keys()) # make a list object based on flavors
cartons = list(ice_cream.values()) # make a list object based on carton count -- assumes 1:1 association!

animals = list(eaters.keys()) 
beasts = list(eaters.values()) 
myfigure = plt.figure(figsize = (10,5)) # generate a object from the figure class, set aspect ratio

# Built the plot
plt.barh(flavors, cartons, color ='orange') 
plt.barh(animals, beasts, color ='green') 
plt.xlabel("Flavors") 
plt.ylabel("Counts: Cartons and Beasts") 
plt.title("Current Ice Cream in Storage") 
plt.show() 

Now using pandas, we can build bar charts a bit easier.

In [5]:
import pandas as pd

my_data = {
    "Flavor": ['Chocolate', 'Strawberry', 'Vanilla'],
    "Number of Cartons": [16, 5, 9]
          }
df = pd.DataFrame(my_data)
df.head()
Out[5]:
Flavor Number of Cartons
0 Chocolate 16
1 Strawberry 5
2 Vanilla 9
In [6]:
df.plot.bar(x='Flavor', y='Number of Cartons', color='magenta' )
Out[6]:
<AxesSubplot:xlabel='Flavor'>
In [7]:
df.plot.bar(x='Flavor', y='Number of Cartons', color="red", rot=45) # rotate the category labels
Out[7]:
<AxesSubplot:xlabel='Flavor'>
In [8]:
import numpy as np 
import matplotlib.pyplot as plt  
  
   
# creating the dataset 
data = {'C':20, 'C++':15, 'Java':30,  
        'Python':35} 
courses = list(data.keys()) 
values = list(data.values()) 
   
fig = plt.figure(figsize = (10, 5)) 
  
# creating the bar plot 
plt.bar(courses, values, color ='maroon',  
        width = 0.4) 
  
plt.xlabel("Courses offered") 
plt.ylabel("No. of students enrolled") 
plt.title("Students enrolled in different courses") 
plt.show() 

Line Charts

A line chart or line plot or line graph or curve chart is a type of chart which displays information as a series of data points called 'markers' connected by straight line segments.

It is a basic type of chart common in many fields. It is similar to a scatter plot (below) except that the measurement points are ordered (typically by their x-axis value) and joined with straight line segments.

A line chart is often used to visualize a trend in data over intervals of time – a time series – thus the line is often drawn chronologically.

The x-axis spacing is sometimes tricky, hence line charts can unintentionally decieve - so be careful that it is the appropriate chart for your application.

Example

Consider the experimental data below

Elapsed Time (s) Speed (m/s)
0 0
1.0 3
2.0 7
3.0 12
4.0 20
5.0 30
6.0 45.6

Show the relationship between time and speed. Is the relationship indicating acceleration? How much?

In [37]:
# Create two lists; time  and speed.
time = [0,1.0,2.0,3.0,4.0,5.0,6.0]
speed = [0,3,7,12,20,30,45.6]
In [38]:
# Create a line chart of speed on y axis and time on x axis
mydata = plt.figure(figsize = (10,5)) # build a square drawing canvass from figure class
plt.plot(time, speed, c='red', marker='v',linewidth=1) # basic line plot
plt.show()
In [54]:
# Estimate acceleration (naive)
dvdt = (max(speed) - min(speed))/(max(time)-min(time))
plottitle = 'Average acceleration %.3f' % (dvdt) + ' m/sec/sec'
seriesnames = ['Data','Model']
modely = [min(speed),max(speed)]
modelx = [min(time),max(time)]
mydata = plt.figure(figsize = (10,5)) # build a square drawing canvass from figure class
plt.plot(time, speed, c='red', marker='v',linewidth=1) # basic line plot
plt.plot(modelx, modely, c='blue',linewidth=1) # basic line plot
plt.xlabel('Time (sec)')
plt.ylabel('Speed (m/sec)')
plt.legend(seriesnames)
plt.title(plottitle)
plt.show()

Scatter Plots

A scatter plot (also called a scatterplot, scatter graph, scatter chart, scattergram, or scatter diagram) is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data. If the points are coded (color/shape/size), one additional variable can be displayed. The data are displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis.

A scatter plot can be used either when one continuous variable that is under the control of the experimenter and the other depends on it or when both continuous variables are independent. If a parameter exists that is systematically incremented and/or decremented by the other, it is called the control parameter or independent variable and is customarily plotted along the horizontal axis. The measured or dependent variable is customarily plotted along the vertical axis. If no dependent variable exists, either type of variable can be plotted on either axis and a scatter plot will illustrate only the degree of correlation (not causation) between two variables.

A scatter plot can suggest various kinds of correlations between variables with a certain confidence interval. For example, weight and height, weight would be on y axis and height would be on the x axis. Correlations may be positive (rising), negative (falling), or null (uncorrelated). If the pattern of dots slopes from lower left to upper right, it indicates a positive correlation between the variables being studied. If the pattern of dots slopes from upper left to lower right, it indicates a negative correlation.

A line of best fit (alternatively called 'trendline') can be drawn in order to study the relationship between the variables. An equation for the correlation between the variables can be determined by established best-fit procedures. For a linear correlation, the best-fit procedure is known as linear regression and is guaranteed to generate a correct solution in a finite time. No universal best-fit procedure is guaranteed to generate a correct solution for arbitrary relationships. A scatter plot is also very useful when we wish to see how two comparable data sets agree and to show nonlinear relationships between variables.

Furthermore, if the data are represented by a mixture model of simple relationships, these relationships will be visually evident as superimposed patterns.

Scatter charts can be built in the form of bubble, marker, or/and line charts.

Much of the above is verbatim/adapted from: https://en.wikipedia.org/wiki/Scatter_plot

In [28]:
# Example 1.  A data file containing heights of fathers, mothers, and sons is to be examined
df = pd.read_csv('galton_subset.csv')
df['child']= df['son'] ; df.drop('son', axis=1, inplace = True) # rename son to child - got to imagine there are some daughters
df.head()
Out[28]:
father mother child
0 78.5 67.0 73.2
1 75.5 66.5 73.5
2 75.0 64.0 71.0
3 75.0 64.0 70.5
4 75.0 58.5 72.0
In [30]:
# build some lists
daddy = df['father'] ; mommy = df['mother'] ; baby = df['child']
In [36]:
myfamily = plt.figure(figsize = (10, 10)) # build a square drawing canvass from figure class
plt.scatter(baby, daddy, c='red') # basic scatter plot
plt.show()
In [33]:
# Looks lousy, needs some labels
myfamily = plt.figure(figsize = (10, 10)) # build a square drawing canvass from figure class
plt.scatter(baby, daddy, c='red' , label='Father') # one plot series
plt.scatter(baby, mommy, c='blue', label='Mother') # two plot series
plt.xlabel("Child's height")
plt.ylabel("Parents' height")
plt.legend()
plt.show() # render the two plots
In [ ]:
 
In [34]:
# Repeat in pandas - The dataframe already is built
df.plot.scatter(x="child", y="father")
Out[34]:
<AxesSubplot:xlabel='child', ylabel='father'>
In [35]:
ax = df.plot.scatter(x="child", y="father", c="red", label='Father')
df.plot.scatter(x="child", y="mother", c="blue", label='Mother', ax=ax)

ax.set_xlabel("Child's height")
ax.set_ylabel("Parents' Height")
Out[35]:
Text(0, 0.5, "Parents' Height")
In [12]:
df.plot.scatter(x="son", y="father")
Out[12]:
<AxesSubplot:xlabel='son', ylabel='father'>
In [13]:
 
Out[13]:
Text(0, 0.5, "Parents' Height")