inclass_assignment_7
September 24, 2025
Data Visualization
• For this assignment, we will be using datasets available through seaborn API to make the
data acquisition easy.
• The goal of this assignment to make you familiar with the seaborn API and learn how to use
it from the given documentation.
• You can work with one or more datasets.
• Tasks:
1. You will show at least one visualization from each feature that are given here:
[Link]
2. Describe the figure with your observations.
• This documentation also provided tutorials. Feel free to see the given examples.
[ ]: #Datasets available to explore from seaborn (Do not use any of these datasets␣
↪for your Project)
import seaborn as sns
sns.get_dataset_names()
[ ]: ['anagrams',
'anscombe',
'attention',
'brain_networks',
'car_crashes',
'diamonds',
'dots',
'dowjones',
'exercise',
'flights',
'fmri',
'geyser',
'glue',
'healthexp',
'iris',
'mpg',
'penguins',
'planets',
'seaice',
1
'taxis',
'tips',
'titanic']
[ ]: #load any Dataset of your choice
penguins = sns.load_dataset('penguins')
[Link]()
[ ]: species island bill_length_mm bill_depth_mm flipper_length_mm \
0 Adelie Torgersen 39.1 18.7 181.0
1 Adelie Torgersen 39.5 17.4 186.0
2 Adelie Torgersen 40.3 18.0 195.0
3 Adelie Torgersen NaN NaN NaN
4 Adelie Torgersen 36.7 19.3 193.0
body_mass_g sex
0 3750.0 Male
1 3800.0 Female
2 3250.0 Female
3 NaN NaN
4 3450.0 Female
[ ]: #Example: Objects
import [Link] as so
(
[Link](penguins, x="bill_length_mm", y="bill_depth_mm")
.add([Link]())
)
[ ]:
2
The scatter plot created using the ‘penguins’ dataset illustrates the relationship between the
“bill_length_mm” and “bill_depth_mm” features. In the plot, “bill_length_mm” is repre-
sented on the x-axis, while “bill_depth_mm” is displayed on the y-axis. It appears that when
“bill_length_mm” is less than 40 mm, the “bill_depth_mm” tends to be comparatively higher.
To explore any potential correlation between these two features, further investigation is needed. A
correlation heatmap could be a useful tool for this analysis.
[8]: #Show a correlation Heatmap among the quantitative features of the 'penguins'␣
↪dataset.
df = sns.load_dataset("penguins")
df['bill_length_mm'] = df['bill_length_mm'].astype(str)
corr = [Link](numeric_only=True)
[Link](figsize=(10, 8))
[Link](corr,
annot=True,
fmt=".2f",
cmap='coolwarm',
square=True,
3
linewidths=0.5,
cbar_kws={"shrink": .8})
[Link]("Correlation Heatmap", fontsize=16)
[Link]()
The correlation Heatmap plot created using the ‘penguins’ dataset illustrates the relationship be-
tween the “bill_length_mm”, “flipper_length_mm”, and body_mass_g features. In the plot,
“bill_length_mm”, “flipper_length_mm”, and body_mass_g” is represented on the x-axis, and
correlates to “bill_length_mm”, “flipper_length_mm”, and body_mass_g displayed on the y-
axis. It appears that when “body_mass_g” is being compare to eachother it exceeds to 1.00. It
also is the same when “bill_depth_mm” and body_mass_g” is compare to eachother as well
[15]: #Objects: Pick a different example than the given one.
import pandas as pd
import numpy as np
4
import seaborn as sns
import [Link] as plt
print([Link]())
species island bill_length_mm bill_depth_mm flipper_length_mm \
0 Adelie Torgersen 39.1 18.7 181.0
1 Adelie Torgersen 39.5 17.4 186.0
2 Adelie Torgersen 40.3 18.0 195.0
3 Adelie Torgersen nan NaN NaN
4 Adelie Torgersen 36.7 19.3 193.0
body_mass_g sex
0 3750.0 Male
1 3800.0 Female
2 3250.0 Female
3 NaN NaN
4 3450.0 Female
In this plot it shows the species of Adelie Torgersen under “bill_length_mm” are 39.1, 39.5, 40.3,
and 36.7. The species of Adelie Torgersen under “bill_depth_mm” are 18.7, 17.4, 18.0, and 19.3.
Thrid the Adelie Torgersen species under “flipper_length_mm” are 181.0, 186.0, 195.0, and 193.0.
Lastly, the species under body_mass_g are 3750.0 male, 3800.0 female, 3250.0 female, and 3450.0
female,
[23]: #Relational plots
import seaborn as sns
sns.set_theme(style="white")
# Load the example mpg dataset
mpg = sns.load_dataset("mpg")
# Plot miles per gallon against horsepower with other semantics
[Link](x="horsepower", y="mpg", hue="origin", size="weight",
sizes=(40, 400), alpha=.5, palette="muted",
height=6, data=mpg)
[23]: <[Link] at 0x782a3d290da0>
5
The Relational plot created using the ‘mpg’ dataset illustrates the relationship between the “horse-
power”and “mpg” . In the plot, “horsepower” is represented on the x-axis, and correlates to “mpg”
displayed on the y-axis. It appears that when the more “horsepower” a car/vehicle has the less the
“mpg” is in these cars.
[24]: #Distribution plots
import seaborn as sns
sns.set_theme(style="darkgrid")
df = sns.load_dataset("penguins")
[Link](
df, x="flipper_length_mm", col="species", row="sex",
binwidth=3, height=3, facet_kws=dict(margin_titles=True),
)
[24]: <[Link] at 0x782a3d1bcda0>
6
The distrubtion plot created using the ‘penguins’ between the “flipper_length_mm” and “count”
features. In the plot, “flipper_length_mm” is represented on the x-axis, while “count” is displayed
on the y-axis. It appears that when “flipper_length_mm” is less than 180 mm, the “count” tends
to be comparatively lower.
[25]: #Categorical plots
import seaborn as sns
sns.set_theme(style="whitegrid", palette="muted")
# Load the penguins dataset
df = sns.load_dataset("penguins")
# Draw a categorical scatterplot to show each observation
ax = [Link](data=df, x="body_mass_g", y="sex", hue="species")
[Link](ylabel="")
[25]: [Text(5.875000000000002, 0.5, '')]
7
This strip plot shows the distribution of penguin body mass (in grams) by sex and species. Male
penguins tend to have higher body mass than females across all species. Gentoo penguins are
significantly heavier than both Adelie and Chinstrap penguins. Adelie and Chinstrap penguins
have overlapping body mass ranges, with Adelie appearing slightly lighter on average.
[26]: #Regression plots
import seaborn as sns
sns.set_theme(style="darkgrid")
tips = sns.load_dataset("tips")
g = [Link](x="total_bill", y="tip", data=tips,
kind="reg", truncate=False,
xlim=(0, 60), ylim=(0, 12),
color="m", height=7)
8
This plot shows the relationship between “total bill” and “tip amount” in a restaurant setting. Each
dot represents a single observation, and the positive trend line suggests that higher total bills are
generally associated with higher tips. The shaded area around the line represents the confidence
interval, indicating uncertainty in the linear regression. On the right, a histogram and KDE plot
display the distribution of total bill amounts, which are more concentrated in the lower range.
[27]: #Multi-plot grids
import seaborn as sns
sns.set_theme(style="ticks")
df = sns.load_dataset("penguins")
[Link](df, hue="species")
9
[27]: <[Link] at 0x782a368b1610>
This image is a multi plot visualizing relationships between different physical measurements of three
penguin species: Adelie (blue), Chinstrap (orange), and Gentoo (green). The diagonal plots show
kernel density estimates for each species’ distribution on individual features: bill depth, flipper
length, and body mass. Off-diagonal scatter plots reveal clear species clusters, especially Gentoo,
which is distinct in flipper length and body mass. The plot demonstrates how these features can
be used to visually separate species based on their morphology.
[28]: #Figure theming
import numpy as np
import seaborn as sns
import [Link] as plt
sns.set_theme(style="dark")
# Simulate data from a bivariate Gaussian
n = 10000
10
mean = [0, 0]
cov = [(2, .4), (.4, .2)]
rng = [Link](0)
x, y = rng.multivariate_normal(mean, cov, n).T
# Draw a combo histogram and scatterplot with density contours
f, ax = [Link](figsize=(6, 6))
[Link](x=x, y=y, s=5, color=".15")
[Link](x=x, y=y, bins=50, pthresh=.1, cmap="mako")
[Link](x=x, y=y, levels=5, color="w", linewidths=1)
[28]: <Axes: >
Figure theming in plotting refers to the customization of visual elements such as colors, fonts,
backgrounds, and gridlines to enhance clarity and aesthetics. It helps align plots with publication
standards or specific style guidelines. Themes can emphasize key data points or improve readability
by controlling distractions and maintaining consistency.
11
[29]: #Color palettes
import numpy as np
import seaborn as sns
import [Link] as plt
sns.set_theme(style="white", context="talk")
rs = [Link](8)
# Set up the matplotlib figure
f, (ax1, ax2, ax3) = [Link](3, 1, figsize=(7, 5), sharex=True)
# Generate some sequential data
x = [Link](list("ABCDEFGHIJ"))
y1 = [Link](1, 11)
[Link](x=x, y=y1, hue=x, palette="rocket", ax=ax1)
[Link](0, color="k", clip_on=False)
ax1.set_ylabel("Sequential")
# Center the data to make it diverging
y2 = y1 - 5.5
[Link](x=x, y=y2, hue=x, palette="vlag", ax=ax2)
[Link](0, color="k", clip_on=False)
ax2.set_ylabel("Diverging")
# Randomly reorder the data to make it qualitative
y3 = [Link](y1, len(y1), replace=False)
[Link](x=x, y=y3, hue=x, palette="deep", ax=ax3)
[Link](0, color="k", clip_on=False)
ax3.set_ylabel("Qualitative")
# Finalize the plot
[Link](bottom=True)
[Link]([Link], yticks=[])
plt.tight_layout(h_pad=2)
12
Color palettes are curated sets of colors used in data visualization to represent different categories,
values, or gradients effectively. They enhance readability, highlight patterns, and ensure visual
appeal while maintaining accessibility, especially for color-blind viewers. Palettes can be sequential,
diverging, or categorical, depending on the type of data being represented.
13