Seaborn is a Python data visualization library built on top of Matplotlib. It provides a high-level interface for creating attractive and informative statistical graphics with minimal code. Seaborn integrates seamlessly with pandas DataFrames, making it an excellent choice for data exploration and analysis.
This tutorial introduces the most commonly used Seaborn plots and demonstrates how to create them.
Common Types of Seaborn Plots
Seaborn provides a variety of plot types for different visualization tasks:
- Line Plot
- Bar Plot
- Scatter Plot
- Histogram
- Box Plot
- Violin Plot
- Count Plot
- Heatmap
- Pair Plot
Install Seaborn
If Seaborn is not installed, use pip:
pip install seaborn
Import the Required Libraries
import seaborn as sns
import matplotlib.pyplot as plt
1. Line Plot
A line plot is used to visualize trends over time or across ordered categories.
Example
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.lineplot(data=tips, x="size", y="total_bill")
plt.show()
Output
The chart displays the relationship between the party size and the average total bill.
2. Bar Plot
A bar plot compares values across different categories.
Example
sns.barplot(
data=tips,
x="day",
y="total_bill"
)
plt.show()
Output
Each bar represents the average total bill for a day of the week.
3. Scatter Plot
Scatter plots visualize relationships between two numerical variables.
Example
sns.scatterplot(
data=tips,
x="total_bill",
y="tip"
)
plt.show()
Output
Each point represents one observation, making it easy to identify trends or outliers.
4. Histogram
Histograms show the distribution of a numerical variable.
Example
sns.histplot(
data=tips,
x="total_bill",
bins=20
)
plt.show()
Output
The histogram displays how frequently different total bill amounts occur.
5. Box Plot
Box plots summarize the distribution of data and highlight outliers.
Example
sns.boxplot(
data=tips,
x="day",
y="total_bill"
)
plt.show()
Output
The box shows the median and quartiles, while points outside the whiskers represent outliers.
6. Violin Plot
A violin plot combines a box plot with a kernel density estimate.
Example
sns.violinplot(
data=tips,
x="day",
y="total_bill"
)
plt.show()
Output
The width of each violin represents the density of observations.
7. Count Plot
A count plot displays the number of observations in each category.
Example
sns.countplot(
data=tips,
x="day"
)
plt.show()
Output
Each bar shows the number of records for a particular day.
8. Heatmap
Heatmaps visualize values using color intensity.
Example
corr = tips.corr(numeric_only=True)
sns.heatmap(
corr,
annot=True,
cmap="coolwarm"
)
plt.show()
Output
The heatmap displays correlations between numerical variables.
9. Pair Plot
A pair plot creates scatter plots and histograms for every numerical feature.
Example
sns.pairplot(tips)
plt.show()
Output
The resulting figure contains multiple plots that help identify relationships among variables.
Customizing Seaborn Plots
Seaborn offers several options for customizing visualizations.
Change the Figure Size
plt.figure(figsize=(10, 5))
Add a Title
plt.title("Monthly Sales")
Change the Color Palette
sns.set_palette("Set2")
Other popular palettes include:
deepmutedbrightpasteldarkcolorblind
Advantages of Seaborn
- Simple and concise syntax.
- Attractive default themes and color palettes.
- Excellent integration with pandas DataFrames.
- Built-in support for statistical visualizations.
- Easy customization while remaining compatible with Matplotlib.
- Ideal for exploratory data analysis and presentation-quality graphics.
Conclusion
Seaborn simplifies the process of creating informative statistical graphics in Python. Whether you need a simple bar chart, a scatter plot to explore relationships, or a heatmap to analyze correlations, Seaborn provides intuitive functions with sensible defaults. Combined with pandas and Matplotlib, it is one of the most widely used visualization libraries in the Python ecosystem.