Seaborn:
1. Introduction to Seaborn
Seaborn is a powerful Python data visualization library built on top of Matplotlib and closely integrated with pandas. It provides a high-level interface for drawing attractive statistical graphics.
Key Features of Seaborn
- Built-in themes for better-looking plots
- Works well with Pandas DataFrames
- Simplifies the creation of complex visualizations
- Provides powerful tools for statistical data visualization
- Allows customization and integration with Matplotlib
2. Installation and Setup
Installing Seaborn
To install Seaborn, use:
pip install seaborn
Or, if using Jupyter Notebook:
!pip install seaborn
Importing Required Libraries
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
3. Seaborn Built-in Datasets
Seaborn provides several built-in datasets. You can load and view these datasets using:
tips = sns.load_dataset("tips")
print(tips.head())
Common Built-in Datasets
- tips – Restaurant tipping data
- iris – Measurements of iris flowers
- penguins – Penguin species data
- titanic – Titanic passenger survival data
4. Basic Seaborn Visualizations
Scatter Plot
sns.scatterplot(x="total_bill", y="tip", data=tips)
plt.show()
Line Plot
sns.lineplot(x="size", y="total_bill", data=tips)
plt.show()
Histogram
sns.histplot(tips["total_bill"], bins=20, kde=True)
plt.show()
Box Plot
sns.boxplot(x="day", y="total_bill", data=tips)
plt.show()
Violin Plot
sns.violinplot(x="day", y="total_bill", data=tips, hue="sex", split=True)
plt.show()
5. Advanced Seaborn Visualizations
Pair Plot
sns.pairplot(tips, hue="sex")
plt.show()
Heatmap (Correlation Matrix)
corr_matrix = tips.corr()
sns.heatmap(corr_matrix, annot=True, cmap="coolwarm", linewidths=0.5)
plt.show()
Catplot
sns.catplot(x="day", y="total_bill", hue="sex", kind="bar", data=tips)
plt.show()
Regression Plot
sns.lmplot(x="total_bill", y="tip", data=tips)
plt.show()
6. Customizing Seaborn Plots
Changing Themes
sns.set_theme(style="darkgrid")
sns.scatterplot(x="total_bill", y="tip", data=tips)
plt.show()
Available Themes
- "darkgrid"
- "whitegrid"
- "dark"
- "white"
- "ticks"
Changing Color Palettes
sns.set_palette("pastel")
sns.boxplot(x="day", y="total_bill", data=tips)
plt.show()
Other Color Palettes
- "deep"
- "muted"
- "bright"
- "colorblind"
7. Combining Seaborn with Matplotlib
plt.figure(figsize=(8, 6))
sns.barplot(x="day", y="total_bill", data=tips)
plt.title("Total Bill Amount by Day")
plt.xlabel("Day of the Week")
plt.ylabel("Total Bill ($)")
plt.show()
8. Saving Seaborn Plots
sns.boxplot(x="day", y="total_bill", data=tips)
plt.savefig("boxplot.png", dpi=300)
9. Conclusion
Seaborn is a powerful data visualization tool that simplifies statistical analysis. It works well with Pandas and Matplotlib, making it an excellent choice for data analysis and storytelling. With its ease of use and customization options, Seaborn is a must-learn library for any data scientist or analyst.