Plot guides › Chart types

Bar chart with error bars and data points

A bar of the mean with error bars, plus each replicate drawn as a dot, shows the summary and the raw data together. Many journals now ask for the individual points when n is small.

Example bar chart with error bars and data points made with matplotlib
Rendered from the code below, with example data.

When to use it

Watch out: Always say in the caption whether error bars are SD, SEM or a 95% CI; they mean very different things.

Ask for it in pltchat

Upload your file and type something like:

"bar chart of mean per treatment with SEM error bars and individual points"

pltchat writes the loader and the plot for your columns, runs it in your browser and shows you the code.

The matplotlib code

Self-contained with example data; swap in pd.read_csv(...) or pd.read_excel(...) for your own file.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(5)
df = pd.DataFrame([{"group": g, "value": rng.normal(mu, 1.2)}
                   for g, mu in [("Control", 5), ("Drug A", 7.5), ("Drug B", 6.2)]
                   for _ in range(6)])
stats = df.groupby("group", sort=False)["value"].agg(["mean", "sem"])

fig, ax = plt.subplots(figsize=(4.5, 4))
x = np.arange(len(stats))
ax.bar(x, stats["mean"], yerr=stats["sem"], capsize=6,
       color="lightgray", edgecolor="k", width=0.6)
for i, g in enumerate(stats.index):
    pts = df.loc[df["group"] == g, "value"]
    ax.scatter(i + rng.uniform(-0.12, 0.12, len(pts)), pts, color="k", s=14, zorder=3)
ax.set_xticks(x, stats.index)
ax.set_ylabel("Response (a.u.)")
ax.spines[["top", "right"]].set_visible(False)
plt.tight_layout()
plt.show()

Make this with your data

Upload a CSV, Excel, JSON or Parquet file. Your first plot is always free.

Related plots