Seaborn is a Python data visualization library built on top of Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics, and it is widely used in data science because it works directly with pandas DataFrames.
Example¶
Here is a complete example of how to use seaborn with the iris dataset:
In [1]:
Copied!
# Import the libraries we need
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style("whitegrid")
# Import the libraries we need
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style("whitegrid")
In [2]:
Copied!
# Load the iris dataset that ships with Seaborn
iris = sns.load_dataset("iris")
iris.head()
# Load the iris dataset that ships with Seaborn
iris = sns.load_dataset("iris")
iris.head()
Out[2]:
| sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
In [3]:
Copied!
# Bar chart: average sepal length by species
sns.barplot(data=iris, x='species', y='sepal_length')
plt.title('Average Sepal Length by Species')
plt.xlabel('Species')
plt.ylabel('Average sepal length')
plt.show()
# Bar chart: average sepal length by species
sns.barplot(data=iris, x='species', y='sepal_length')
plt.title('Average Sepal Length by Species')
plt.xlabel('Species')
plt.ylabel('Average sepal length')
plt.show()
In [4]:
Copied!
# Histogram of sepal length
sns.histplot(data=iris, x='sepal_length', kde=True)
plt.title('Sepal Length Histogram')
plt.xlabel('Sepal length')
plt.ylabel('Frequency')
plt.show()
# Histogram of sepal length
sns.histplot(data=iris, x='sepal_length', kde=True)
plt.title('Sepal Length Histogram')
plt.xlabel('Sepal length')
plt.ylabel('Frequency')
plt.show()
In [5]:
Copied!
# Line chart: petal length across the row index
sns.lineplot(data=iris, x=iris.index, y='petal_length', hue='species')
plt.title('Petal Length Across Rows')
plt.xlabel('Index')
plt.ylabel('Petal length')
plt.show()
# Line chart: petal length across the row index
sns.lineplot(data=iris, x=iris.index, y='petal_length', hue='species')
plt.title('Petal Length Across Rows')
plt.xlabel('Index')
plt.ylabel('Petal length')
plt.show()
In [6]:
Copied!
# Scatter plot: sepal length vs. sepal width
sns.scatterplot(data=iris, x='sepal_length', y='sepal_width', hue='species')
plt.title('Scatter Plot: Sepal Length vs. Sepal Width')
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.show()
# Scatter plot: sepal length vs. sepal width
sns.scatterplot(data=iris, x='sepal_length', y='sepal_width', hue='species')
plt.title('Scatter Plot: Sepal Length vs. Sepal Width')
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.show()
In [7]:
Copied!
# Box plot: distribution of petal length by species
sns.boxplot(data=iris, x='species', y='petal_length')
plt.title('Box Plot: Petal Length by Species')
plt.xlabel('Species')
plt.ylabel('Petal length')
plt.show()
# Box plot: distribution of petal length by species
sns.boxplot(data=iris, x='species', y='petal_length')
plt.title('Box Plot: Petal Length by Species')
plt.xlabel('Species')
plt.ylabel('Petal length')
plt.show()
In [8]:
Copied!
# Violin plot: distribution of petal length by species
sns.violinplot(data=iris, x='species', y='petal_length', inner='quart')
plt.title('Violin Plot: Petal Length by Species')
plt.xlabel('Species')
plt.ylabel('Petal length')
plt.show()
# Violin plot: distribution of petal length by species
sns.violinplot(data=iris, x='species', y='petal_length', inner='quart')
plt.title('Violin Plot: Petal Length by Species')
plt.xlabel('Species')
plt.ylabel('Petal length')
plt.show()
In [9]:
Copied!
# One scatter plot per species, side by side
g = sns.FacetGrid(iris, col='species', height=4)
g.map(sns.scatterplot, 'sepal_length', 'sepal_width', alpha=0.7)
g.set_axis_labels('Sepal length', 'Sepal width')
g.set_titles(col_template='{col_name}')
plt.show()
# One scatter plot per species, side by side
g = sns.FacetGrid(iris, col='species', height=4)
g.map(sns.scatterplot, 'sepal_length', 'sepal_width', alpha=0.7)
g.set_axis_labels('Sepal length', 'Sepal width')
g.set_titles(col_template='{col_name}')
plt.show()