1.1 MiB
Scatter Plots¶
Scatter plots can show how different features are related to one another, the main theme between all relational plot types is they display how features are interconnected to each other. There are many different types of plots that can be used to show this, so let's explore the scatterplot() as well as general seaborn parameters applicable to other plot types.
import pandas as pd
import seaborn as sns
df = pd.read_csv("dm_office_sales.csv")
df.head()
df.info()
Scatterplot¶
sns.scatterplot(x='salary',y='sales',data=df)
Connecting to Figure in Matplotlib¶
Note how matplotlib is still connected to seaborn underneath (even without importing matplotlib.pyplot), since seaborn itself is directly making a Figure call with matplotlib. We can import matplotlib.pyplot and make calls to directly effect the seaborn figure.
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))
sns.scatterplot(x='salary',y='sales',data=df)
plt.figure(figsize=(12,8))
sns.scatterplot(x='salary',y='sales',data=df,hue='division')
plt.figure(figsize=(12,8))
sns.scatterplot(x='salary',y='sales',data=df,hue='work experience')
Choosing a palette from Matplotlib's cmap: https://matplotlib.org/tutorials/colors/colormaps.html
plt.figure(figsize=(12,8))
sns.scatterplot(x='salary',y='sales',data=df,hue='work experience',palette='viridis')
plt.figure(figsize=(12,8))
sns.scatterplot(x='salary',y='sales',data=df,size='work experience')
Use s= if you want to change the marker size to be some uniform integer value
plt.figure(figsize=(12,8))
sns.scatterplot(x='salary',y='sales',data=df,s=200)
plt.figure(figsize=(12,8))
sns.scatterplot(x='salary',y='sales',data=df,s=200,linewidth=0,alpha=0.2)
style¶
Automatically choose styles based on another categorical feature in the dataset. Optionally use the markers= parameter to pass a list of marker choices based off matplotlib, for example: ['*','+','o']
plt.figure(figsize=(12,8))
sns.scatterplot(x='salary',y='sales',data=df,style='level of education')
plt.figure(figsize=(12,8))
# Sometimes its nice to do BOTH hue and style off the same column
sns.scatterplot(x='salary',y='sales',data=df,style='level of education',hue='level of education',s=100)
Exporting a Seaborn Figure¶
plt.figure(figsize=(12,8))
sns.scatterplot(x='salary',y='sales',data=df,style='level of education',hue='level of education',s=100)
# Call savefig in the same cell
plt.savefig('example_scatter.jpg')