duration
variable
balance
variable (logarithmic scale)
education
variableeducation
variable (redundant
coding)
Installing Matplotlib and seaborn with conda (recommended)
% conda install matplotlib seaborn
Installing Matplotlib and seaborn with pip
% pip install matplotlib seaborn
Importing Matplotlib and seaborn (in Python scripts or notebooks)
>>> import seaborn as sns
>>> import matplotlib.pyplot as plt
sns.set_style("ticks") # set seaborn style
sns.set_context("talk") # set seaborn context
fig, ax = plt.subplots(figsize=[8, 5]) # set figure size
plot = sns.scatterplot(
data=bank_sample,
x="duration", # variable to map to x axis
y="balance", # variable to map to y axis
style="education", # variable to map to shape
hue="education", # variable to map to color
alpha=0.5 # transparency
)
plot.set(yscale="log") # use log scale for y axis
# set axis and legend titles
plt.xlabel("Last contact duration (seconds)")
plt.ylabel("Balance ($)")
plt.legend(title="Education")
plt.savefig("dataviz_example.svg") # save to disk
histplot()
function (cf.
documentation)
fig, ax = plt.subplots(figsize=[7, 5])
sns.histplot(
data=bank,
x="age",
binwidth=5, # 5-year bins
color="steelblue",
edgecolor="black")
ax.set( # set plot title and axis labels
title="Age distribution",
xlabel="Age (years)",
ylabel="Frequency"
)
sns.despine(offset=5, trim=True)
plt.savefig("histograms.svg")
kdeplot()
function (cf.
documentation)
fig, ax = plt.subplots(figsize=[7, 5])
sns.kdeplot(
data=bank,
x="age",
color="steelblue"
)
ax.set(
title="Age distribution",
xlabel="Age (years)"
)
sns.despine(offset=5)
plt.savefig("dataviz-density.svg")
boxplot()
)violinplot()
)boxplot()
function (cf.
documentation)
fig, ax = plt.subplots(figsize=(8, 5))
sns.boxplot(
data=bank,
x="marital",
y="age",
width=.3
)
ax.set(
title="Age distribution by marital status",
xlabel="Marital status",
ylabel="Age (years)"
)
sns.despine()
violinplot()
function
(cf.
documentation)
fig, ax = plt.subplots(figsize=(8, 5))
sns.violinplot(
data=bank,
x="marital",
y="age",
width=.5
# hue="marital"
)
ax.set(
title="Age distribution by marital status",
xlabel="Marital status",
ylabel="Age (years)"
)
sns.despine()
scatterplot()
)barplot()
(cf.
documentation)
countplot()
instead (cf.
documentation)
fig, ax = plt.subplots(figsize=(8, 5))
sns.countplot(
data=bank,
x="marital"
)
ax.set(
title="Number of clients per marital status",
xlabel="Marital status",
ylabel="Count"
)
sns.despine()
set_theme()
,
set_style()
, and set_context()
functions (cf.
documentation)
sns.set_style("white")
...
sns.set_style("whitegrid")
...
sns.set_style("dark")
...
sns.set_style("darkgrid")
...
set_theme()
,
set_style()
, and set_context()
functions (cf.
documentation)
sns.set_context("paper")
...
sns.set_context("notebook")
...
sns.set_context("talk")
...
sns.set_context("poster")
...