Ch. 7 Seaborn
Last update: Thu Nov 5 19:22:01 2020 -0600 (5124cef)
R
library(reticulate)
use_condaenv("r-python", required = TRUE)
7.1 Error bands
Python
# https://seaborn.pydata.org/examples/errorband_lineplots.html
import matplotlib.pyplot as plt
import seaborn as sns
="darkgrid")
sns.set_theme(style
# Load an example dataset with long-form data
= sns.load_dataset("fmri")
fmri
# Plot the responses for different events and regions
="timepoint", y="signal",
sns.lineplot(x="region", style="event",
hue=fmri)
data plt.show()
7.2 Boxplots
Python
# https://seaborn.pydata.org/examples/horizontal_boxplot.html
import seaborn as sns
import matplotlib.pyplot as plt
="ticks")
sns.set_theme(style
# Initialize the figure with a logarithmic x axis
= plt.subplots(figsize=(7, 6))
f, ax "log")
ax.set_xscale(
# Load the example planets dataset
= sns.load_dataset("planets")
planets # Plot the orbital period with horizontal boxes
="distance", y="method", data=planets,
sns.boxplot(x=[0, 100], width=.6, palette="vlag")
whis# Add in points to show each observation
="distance", y="method", data=planets,
sns.stripplot(x=4, color=".3", linewidth=0)
size# Tweak the visual presentation
True)
ax.xaxis.grid(set(ylabel="")
ax.=True, left=True)
sns.despine(trim
plt.show()
7.3 Facets
Python
# https://seaborn.pydata.org/examples/timeseries_facets.html
import matplotlib.pyplot as plt
import seaborn as sns
="dark")
sns.set_theme(style= sns.load_dataset("flights")
flights
# Plot each year's time series in its own facet
= sns.relplot(
g =flights,
data="month", y="passengers", col="year", hue="year",
x="line", palette="crest", linewidth=4, zorder=5,
kind=3, height=2, aspect=1.5, legend=False,
col_wrap
)
# Iterate over each subplot to customize further
for year, ax in g.axes_dict.items():
# Add the title as an annotation within the plot
.8, .85, year, transform=ax.transAxes, fontweight="bold")
ax.text(
# Plot every year's time series in the background
sns.lineplot(=flights, x="month", y="passengers", units="year",
data=None, color=".7", linewidth=1, ax=ax,
estimator
)# Reduce the frequency of the x axis ticks
2])
ax.set_xticks(ax.get_xticks()[::# Tweak the supporting aspects of the plot
"")
g.set_titles("", "Passengers")
g.set_axis_labels(
g.tight_layout()
plt.show()
Python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
="darkgrid")
sns.set_theme(style
= sns.load_dataset("tips")
tips = sns.load_dataset("fmri")
fmri
="timepoint", y="signal", hue="event", style="event",
sns.relplot(x="subject", col_wrap=5,
col=3, aspect=.75, linewidth=2.5,
height="line", data=fmri.query("region == 'frontal'"));
kind
plt.show()