doc/_tutorial/error_bars.ipynb
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
sns.set_theme(style="darkgrid")
np.random.seed(sum(map(ord, "errorbars")))
import io
from IPython.display import SVG
f = mpl.figure.Figure(figsize=(8, 5))
axs = f.subplots(2, 2, sharex=True, sharey=True,)
plt.setp(axs, xlim=(-1, 1), ylim=(-1, 1), xticks=[], yticks=[])
for ax, color in zip(axs.flat, ["C0", "C2", "C3", "C1"]):
ax.set_facecolor(mpl.colors.to_rgba(color, .25))
kws = dict(x=0, y=.2, ha="center", va="center", size=18)
axs[0, 0].text(s="Standard deviation", **kws)
axs[0, 1].text(s="Standard error", **kws)
axs[1, 0].text(s="Percentile interval", **kws)
axs[1, 1].text(s="Confidence interval", **kws)
kws = dict(x=0, y=-.2, ha="center", va="center", size=18, font="Courier New")
axs[0, 0].text(s='errorbar=("sd", scale)', **kws)
axs[0, 1].text(s='errorbar=("se", scale)', **kws)
axs[1, 0].text(s='errorbar=("pi", width)', **kws)
axs[1, 1].text(s='errorbar=("ci", width)', **kws)
kws = dict(size=18)
axs[0, 0].set_title("Spread", **kws)
axs[0, 1].set_title("Uncertainty", **kws)
axs[0, 0].set_ylabel("Parametric", **kws)
axs[1, 0].set_ylabel("Nonparametric", **kws)
f.tight_layout()
f.subplots_adjust(hspace=.05, wspace=.05 * (4 / 6))
f.savefig(svg:=io.StringIO(), format="svg")
SVG(svg.getvalue())
def plot_errorbars(arg, **kws):
np.random.seed(sum(map(ord, "error_bars")))
x = np.random.normal(0, 1, 100)
f, axs = plt.subplots(2, figsize=(7, 2), sharex=True, layout="tight")
sns.pointplot(x=x, errorbar=arg, **kws, capsize=.3, ax=axs[0])
sns.stripplot(x=x, jitter=.3, ax=axs[1])
plot_errorbars("sd")
plot_errorbars(("pi", 50))
plot_errorbars("se")
plot_errorbars("ci")
plot_errorbars(("se", 2))
plot_errorbars("ci", estimator="median")
plot_errorbars("ci", n_boot=5000, seed=10)
plot_errorbars(lambda x: (x.min(), x.max()))
x = np.random.normal(0, 1, 50)
y = x * 2 + np.random.normal(0, 2, size=x.size)
sns.regplot(x=x, y=y)