An Introduction To Seaborn
An Introduction To Seaborn
Dr. Atif Tahir, Dr. Abdul Aziz and Dr. Nuaman Durrani
Introduction
• Seaborn is a complement to matplotlib and not a replacement for it
6- Add A Title:
set_title()
Loading A Built-in Seaborn
Data Set
import seaborn as sns
import matplotlib.pyplot as plt
# Load iris data
iris = sns.load_dataset("iris")
# Construct iris plot
sns.swarmplot(x="species", y="petal_length", data=iris)
# Show plot
plt.show()
Loading Your Data from Pandas DataFrame
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# Load in data
tips = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-
data/master/tips.csv")
# Create violinplot
sns.violinplot(x = "total_bill", data=tips)
# Show the plot
plt.show()
1- Regression Plot
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
sns.lmplot(x='total_bill', y='tip', data=tips)
plt.show()
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')sns.
plt.show()
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')sns.
lmplot(x='total_bill', y='tip', hue='sex', data=tips, col = 'sex')
plt.show()
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()# Load the example tips dataset
iris = sns.load_dataset("iris")
# Plot tip as a function of toal bill across days
g = sns.lmplot(x="sepal_length", y="sepal_width",
hue="species",truncate=True, size=5, data=iris)
# Use more informative axis labels than are provided by default
g.set_axis_labels("Sepal length (mm)", "Sepal width (mm)")
plt.show()
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")
# Load the example dataset for Anscombe's quartet
df = sns.load_dataset("anscombe")
# Show the results of a linear regression within each dataset
sns.lmplot(x="x", y="y", col="dataset", hue="dataset", data=df,col_wrap=2,
ci=None, palette="muted", size=4,scatter_kws={"s": 50, "alpha": 1})
plt.show()
2- Distribution Plot
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')
sns.distplot(tips['total_bill'], kde=False, bins=30)
plt.show()
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="white", palette="muted", color_codes=True)
rs = np.random.RandomState(10)
# Set up the matplotlib figure
f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
sns.despine(left=True)
# Generate a random univariate dataset
d = rs.normal(size=100)
# Plot a simple histogram with binsize determined automatically
sns.distplot(d, kde=False, color="b", ax=axes[0, 0])
# Plot a kernel density estimate and rug plot
sns.distplot(d, hist=False, rug=True, color="r", ax=axes[0, 1])
# Plot a filled kernel density estimate
sns.distplot(d, hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0])
# Plot a historgram and kernel density estimate
sns.distplot(d, color="m", ax=axes[1, 1])
plt.setp(axes, yticks=[])
plt.tight_layout()
plt.show()
3- Joint Plot
• We can combine different types of plots together to create a join plot
such as scatter, hex, regression…etc.