Python Seaborn Module
Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. It integrates closely with pandas data structures.
Installation
pip install seaborn
Importing the Module
import seaborn as sns
import matplotlib.pyplot as plt
Basic Plotting
Seaborn comes with built-in datasets which are useful for practice.
# Load an example dataset
tips = sns.load_dataset("tips")
Scatter Plot (scatterplot)
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time")
plt.show()
Line Plot (lineplot)
sns.lineplot(data=tips, x="total_bill", y="tip")
plt.show()
Histogram (histplot)
sns.histplot(data=tips, x="total_bill", kde=True)
plt.show()
Box Plot (boxplot)
sns.boxplot(data=tips, x="day", y="total_bill")
plt.show()
Heatmap (heatmap)
Heatmaps are great for visualizing correlation matrices.
# Calculate correlation matrix (only numeric columns)
corr = tips.corr(numeric_only=True)
sns.heatmap(corr, annot=True, cmap="coolwarm")
plt.show()
Pair Plot (pairplot)
Plot pairwise relationships in a dataset. This creates a grid of Axes such that each variable in data will by shared in the y-axis across a single row and in the x-axis across a single column.
sns.pairplot(tips, hue="sex")
plt.show()
Themes
Seaborn makes it easy to change the overall look of your plots using set_theme.
sns.set_theme(style="darkgrid")
# Available styles: darkgrid, whitegrid, dark, white, ticks