Plotting#

Prerequisites

Outcomes

  • Understand components of matplotlib plots

  • Make basic plots

# Uncomment following line to install on colab
#! pip install 

Visualization#

One of the most important outputs of your analysis will be the visualizations that you choose to communicate what you’ve discovered.

Here are what some people – whom we think have earned the right to an opinion on this material – have said with respect to data visualizations.

I spend hours thinking about how to get the story across in my visualizations. I don’t mind taking that long because it’s that five minutes of presenting it or someone getting it that can make or break a deal – Goldman Sachs executive

We won’t have time to cover “how to make a compelling data visualization” in this lecture.

Instead, we will focus on the basics of creating visualizations in Python.

This will be a fast introduction, but this material appears in almost every lecture going forward, which will help the concepts sink in.

In almost any profession that you pursue, much of what you do involves communicating ideas to others.

Data visualization can help you communicate these ideas effectively, and we encourage you to learn more about what makes a useful visualization.

We include some references that we have found useful below.

matplotlib#

The most widely used plotting package in Python is matplotlib.

The standard import alias is

import matplotlib.pyplot as plt
import numpy as np

Note above that we are using matplotlib.pyplot rather than just matplotlib.

pyplot is a sub-module found in some large packages to further organize functions and types. We are able to give the plt alias to this sub-module.

Additionally, when we are working in the notebook, we need tell matplotlib to display our images inside of the notebook itself instead of creating new windows with the image.

This is done by

%matplotlib inline

The commands with % before them are called Magics.

First Plot#

Let’s create our first plot!

After creating it, we will walk through the steps one-by-one to understand what they do.

# Step 1
fig, ax = plt.subplots()

# Step 2
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

# Step 3
ax.plot(x, y)
[<matplotlib.lines.Line2D at 0x7f0e6822a640>]
../_images/1bf7feda8b1a60125c48001db93e288c95fc7eac7ae5572966a0a1d9e1f3c639.png
  1. Create a figure and axis object which stores the information from our graph.

  2. Generate data that we will plot.

  3. Use the x and y data, and make a line plot on our axis, ax, by calling the plot method.

Difference between Figure and Axis#

We’ve found that the easiest way for us to distinguish between the figure and axis objects is to think about them as a framed painting.

The axis is the canvas; it is where we “draw” our plots.

The figure is the entire framed painting (which inclues the axis itself!).

We can also see this by setting certain elements of the figure to different colors.

fig, ax = plt.subplots()

fig.set_facecolor("red")
ax.set_facecolor("blue")
../_images/4c09b55b6b6cd6171c50e64e5fdec99f71c5b2d6bb06911903e88e62da644324.png

This difference also means that you can place more than one axis on a figure.

# We specified the shape of the axes -- It means we will have two rows and three columns
# of axes on our figure
fig, axes = plt.subplots(2, 3)

fig.set_facecolor("gray")

# Can choose hex colors
colors = ["#065535", "#89ecda", "#ffd1dc", "#ff0000", "#6897bb", "#9400d3"]

# axes is a numpy array and we want to iterate over a flat version of it
for (ax, c) in zip(axes.flat, colors):
    ax.set_facecolor(c)

fig.tight_layout()
../_images/0946d5c19b2c655058b2cb89cf976b7226487958662c2c1cfdc096bb01228494.png

Functionality#

The matplotlib library is versatile and very flexible.

You can see various examples of what it can do on the matplotlib example gallery.

We work though a few examples to quickly introduce some possibilities.

Bar

countries = ["CAN", "MEX", "USA"]
populations = [36.7, 129.2, 325.700]
land_area = [3.850, 0.761, 3.790]

fig, ax = plt.subplots(2)

ax[0].bar(countries, populations, align="center")
ax[0].set_title("Populations (in millions)")

ax[1].bar(countries, land_area, align="center")
ax[1].set_title("Land area (in millions miles squared)")

fig.tight_layout()
../_images/3e49fc481470ab06be6349ec9cecd72b57164a201d0910e4976803b24323ccb5.png

Scatter and annotation

N = 50

np.random.seed(42)

x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radii

fig, ax = plt.subplots()

ax.scatter(x, y, s=area, c=colors, alpha=0.5)

ax.annotate(
    "First point", xy=(x[0], y[0]), xycoords="data",
    xytext=(25, -25), textcoords="offset points",
    arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0.6")
)
Text(25, -25, 'First point')
../_images/cfe5fca77a5cbebd082bcab15b898743e798587beb50787f181f445eda4e10c8.png

Fill between

x = np.linspace(0, 1, 500)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)

fig, ax = plt.subplots()

ax.grid(True)
ax.fill(x, y)
[<matplotlib.patches.Polygon at 0x7f0e5ad1dac0>]
../_images/ca152af2136c662e772386d1caf781735de080749ae49053ca4c6c32767cfb9d.png