v Making A Matplotlib Scatterplot From A Pandas Dataframe - Python

Making A Matplotlib Scatterplot From A Pandas Dataframe

Based on: StackOverflow.

import modules

%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

Create dataframe

raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
        'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'], 
        'female': [0, 1, 1, 0, 1],
        'age': [42, 52, 36, 24, 73], 
        'preTestScore': [4, 24, 31, 2, 3],
        'postTestScore': [25, 94, 57, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'female', 'preTestScore', 'postTestScore'])
df
first_name last_name age female preTestScore postTestScore
0 Jason Miller 42 0 4 25
1 Molly Jacobson 52 1 24 94
2 Tina Ali 36 1 31 57
3 Jake Milner 24 0 2 62
4 Amy Cooze 73 1 3 70

Scatterplot of preTestScore and postTestScore, with the size of each point determined by age

plt.scatter(df.preTestScore, df.postTestScore
, s=df.age)
<matplotlib.collections.PathCollection at 0x10ca42b00>

png

Scatterplot of preTestScore and postTestScore with the size = 300 and the color determined by sex

plt.scatter(df.preTestScore, df.postTestScore, s=300, c=df.female)
<matplotlib.collections.PathCollection at 0x10cb90a90>

png