Seaborn_Visualization

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

1/7/25, 10:30 AM Seaborn_Visualization

Relational Plots

Scatter Plot
In [1]: import seaborn as sns
import matplotlib.pyplot as plt

# Load dataset
tips = sns.load_dataset("tips")

# Scatter Plot
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time", style="se
plt.title("Scatter Plot: Total Bill vs Tip")
plt.show()

Line Plot
In [2]: # Load dataset
fmri = sns.load_dataset("fmri")

# Line Plot
sns.lineplot(data=fmri, x="timepoint", y="signal", hue="event", style="re
plt.title("Line Plot: Signal Over Time")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 1/18
1/7/25, 10:30 AM Seaborn_Visualization

Categorical Plots

Bar Plot
In [3]: # Load dataset
titanic = sns.load_dataset("titanic")

# Bar Plot
sns.barplot(data=titanic, x="class", y="survived", hue="sex")
plt.title("Bar Plot: Survival Rate by Class and Gender")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 2/18
1/7/25, 10:30 AM Seaborn_Visualization

Count Plot
In [12]: # Count plot
sns.countplot(data=tips, x="day", hue="sex", palette="Set2")
plt.title("Count Plot of Days by Gender")
plt.legend(title="Gender")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 3/18
1/7/25, 10:30 AM Seaborn_Visualization

Box Plot
In [4]: # Box Plot
sns.boxplot(data=tips, x="day", y="total_bill", hue="smoker")
plt.title("Box Plot: Total Bill Distribution by Day and Smoking")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 4/18
1/7/25, 10:30 AM Seaborn_Visualization

Violin Plot
In [5]: # Violin Plot
sns.violinplot(data=tips, x="day", y="total_bill", hue="sex", split=True)
plt.title("Violin Plot: Total Bill Distribution by Day and Gender")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 5/18
1/7/25, 10:30 AM Seaborn_Visualization

Strip Plot
In [10]: import seaborn as sns
import matplotlib.pyplot as plt

# Load dataset
tips = sns.load_dataset("tips")

# Strip plot
sns.stripplot(data=tips, x="day", y="total_bill", jitter=True, hue="sex",
plt.title("Strip Plot of Total Bill by Day")
plt.legend(title="Gender")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 6/18
1/7/25, 10:30 AM Seaborn_Visualization

Swarm Plot
In [11]: # Swarm plot
sns.swarmplot(data=tips, x="day", y="total_bill", hue="sex", palette="Set
plt.title("Swarm Plot of Total Bill by Day")
plt.legend(title="Gender")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 7/18
1/7/25, 10:30 AM Seaborn_Visualization

Distribution Plot

Histogram
In [15]: # Histogram of petal length
sns.histplot(data=iris, x="petal_length", hue="species", kde=True, bins=2
plt.title("Histogram of Petal Length by Species")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 8/18
1/7/25, 10:30 AM Seaborn_Visualization

KDE
In [14]: # KDE plot for sepal length
sns.kdeplot(data=iris, x="sepal_length", hue="species", fill=True, common
plt.title("KDE Plot of Sepal Length by Species")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 9/18
1/7/25, 10:30 AM Seaborn_Visualization

ECDF
In [16]: # ECDF of petal width
sns.ecdfplot(data=iris, x="petal_width", hue="species", palette="Set2")
plt.title("ECDF Plot of Petal Width by Species")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 10/18
1/7/25, 10:30 AM Seaborn_Visualization

Matrix Plot

Heatmap
In [9]: import seaborn as sns
import matplotlib.pyplot as plt

# Load dataset
tips = sns.load_dataset("tips")

# Select only numerical columns for correlation matrix


numerical_tips = tips.select_dtypes(include=["float64", "int64"])

# Calculate correlation matrix


corr = numerical_tips.corr()

# Plot heatmap
sns.heatmap(corr, annot=True, cmap="coolwarm", fmt=".2f")
plt.title("Correlation Heatmap")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 11/18
1/7/25, 10:30 AM Seaborn_Visualization

Cluster Map
In [13]: import seaborn as sns
import matplotlib.pyplot as plt

# Load dataset
iris = sns.load_dataset("iris")

# Compute a correlation matrix


corr = iris.drop("species", axis=1).corr()

# Create a cluster map


sns.clustermap(corr, annot=True, cmap="coolwarm", figsize=(8, 6))
plt.title("Cluster Map of Iris Dataset", pad=20)
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 12/18
1/7/25, 10:30 AM Seaborn_Visualization

Pair Plot
In [7]: # Load dataset
iris = sns.load_dataset("iris")

# Pair Plot
sns.pairplot(iris, hue="species", diag_kind="kde")
plt.suptitle("Pair Plot: Iris Dataset", y=1.02)
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 13/18
1/7/25, 10:30 AM Seaborn_Visualization

Multi-plot Grids

Facet Grid
In [8]: # Facet Grid
g = sns.FacetGrid(tips, col="time", row="sex", margin_titles=True)
g.map(sns.scatterplot, "total_bill", "tip")
plt.subplots_adjust(top=0.9)
g.fig.suptitle("Facet Grid: Total Bill vs Tip")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 14/18
1/7/25, 10:30 AM Seaborn_Visualization

Joint Plot
In [19]: # Joint plot
sns.jointplot(data=tips, x="total_bill", y="tip", kind="reg", height=8, r
plt.suptitle("Joint Plot of Tips vs Total Bill", y=1.02)
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 15/18
1/7/25, 10:30 AM Seaborn_Visualization

Regression Plots
In [18]: # Regression plot
sns.regplot(data=tips, x="total_bill", y="tip", scatter_kws={"alpha": 0.6
plt.title("Regression Plot of Tips vs Total Bill")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 16/18
1/7/25, 10:30 AM Seaborn_Visualization

Residual Plot
In [17]: import seaborn as sns
import matplotlib.pyplot as plt

# Load dataset
tips = sns.load_dataset("tips")

# Residual plot
sns.residplot(data=tips, x="total_bill", y="tip", lowess=True, line_kws={
plt.title("Residual Plot of Tips vs Total Bill")
plt.show()

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 17/18
1/7/25, 10:30 AM Seaborn_Visualization

file:///home/m230788ec/Downloads/Seaborn_Visualization.html 18/18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy