Seaborn Cheat Sheet

Here’s a Seaborn cheat sheet to help you quickly reference common functions and techniques when working with Seaborn, a popular Python data visualization library based on Matplotlib.

Installation

  • Install Seaborn using pip:
pip install seaborn

Importing Seaborn

import seaborn as sns

Setting Styles

  • Choose a Seaborn style:
sns.set_style("whitegrid")  # Other options: "darkgrid", "white", "dark", "ticks"

Loading Datasets

  • Seaborn comes with built-in datasets for practice:
tips = sns.load_dataset("tips")

Plotting Univariate Distributions

  • Histogram:
sns.histplot(data=tips, x="total_bill", bins=20, kde=True, color="skyblue")

Kernel Density Estimate (KDE) Plot:

sns.kdeplot(data=tips, x="total_bill", fill=True, color="skyblue")

Plotting Bivariate Distributions

  • Scatter Plot:
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex", palette="viridis")

Joint Plot:

sns.jointplot(data=tips, x="total_bill", y="tip", kind="scatter", marginal_kws=dict(bins=15, fill=False))

Categorical Plots

  • Box Plot:
sns.boxplot(data=tips, x="day", y="total_bill", hue="sex", palette="pastel")

Violin Plot:

sns.violinplot(data=tips, x="day", y="total_bill", hue="sex", split=True, inner="quart", palette="pastel")

Count Plot:

sns.countplot(data=tips, x="day", hue="sex", palette="pastel")

Pair Plots

  • Pair Plot:
sns.pairplot(tips, hue="sex", palette="husl")

Heatmaps

  • Correlation Heatmap:
correlation_matrix = tips.corr()
sns.heatmap(correlation_matrix, annot=True, cmap="coolwarm")

Regression Plots

  • Linear Regression Plot:
sns.regplot(data=tips, x="total_bill", y="tip", scatter_kws={"s": 15}, line_kws={"color": "orange"})

Residual Plot:

sns.residplot(data=tips, x="total_bill", y="tip", scatter_kws={"s": 15}, color="purple")

Customizing Plots

  • Setting Labels and Title:
plt.xlabel("Total Bill ($)")
plt.ylabel("Tip ($)")
plt.title("Tip vs Total Bill")

Adjusting Figure Size:

plt.figure(figsize=(10, 6))

Saving Plots

plt.savefig("plot.png", dpi=300, bbox_inches="tight")

This cheat sheet covers fundamental Seaborn functions for data visualization. For more detailed information and advanced usage, refer to the official Seaborn documentation. Customize these functions based on your specific visualization requirements.