Matplotlib and seaborn 3 cheatsheet

Matplotlib and seaborn 3: Layout and styling

6 common Matplotlib and seaborn 3 tasks.

Draw several charts

Create a grid of axes and plot on each one.

fig, axes = plt.subplots(1, 2, figsize=(10, 4))
axes[0].plot(x, np.sin(x))
axes[1].plot(x, np.cos(x))
fig.tight_layout()

Two y-axes

Plot two series with different scales on the same chart.

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x, x ** 2, color="tab:blue")
ax2.plot(x, np.sqrt(x), color="tab:red")

Use a log scale

Plot values that span several orders of magnitude.

fig, ax = plt.subplots()
ax.plot(x + 1, np.exp(x))
ax.set_yscale("log")

Annotate a point

Add a label with an arrow pointing at a data point.

fig, ax = plt.subplots()
ax.plot(x, np.sin(x))
ax.annotate("peak", xy=(np.pi / 2, 1), xytext=(3, 0.5), arrowprops={"arrowstyle": "->"})

Apply a style

Use a built-in style sheet for consistent looks.

plt.style.use("ggplot")

Rotate tick labels

Rotate crowded labels on the x-axis.

plt.xticks(rotation=45, ha="right")

Was this page helpful?