Title: Creating Scatterplots with Seaborn
Slug: seaborn_scatterplot
Summary: Creating Scatterplots with Seaborn
Date: 2016-05-01 12:00
Category: Python
Tags: Data Visualization
Authors: Chris Albon
In [1]:
import pandas as pd
%matplotlib inline
import random
import matplotlib.pyplot as plt
import seaborn as sns
In [2]:
# Create empty dataframe
df = pd.DataFrame()
# Add columns
df['x'] = random.sample(range(1, 1000), 5)
df['y'] = random.sample(range(1, 1000), 5)
df['z'] = [1,0,0,1,0]
df['k'] = ['male','male','male','female','female']
In [3]:
# View first few rows of data
df.head()
Out[3]:
In [4]:
# Set style of scatterplot
sns.set_context("notebook", font_scale=1.1)
sns.set_style("ticks")
# Create scatterplot of dataframe
sns.lmplot('x', # Horizontal axis
'y', # Vertical axis
data=df, # Data source
fit_reg=False, # Don't fix a regression line
hue="z", # Set color
scatter_kws={"marker": "D", # Set marker style
"s": 100}) # S marker size
# Set title
plt.title('Histogram of IQ')
# Set x-axis label
plt.xlabel('Time')
# Set y-axis label
plt.ylabel('Deaths')
Out[4]: