Matplotlib Cheatsheet

kuniga.me > Docs > Matplotlib Cheatsheet

Matplotlib Cheatsheet

Syntax for common tasks I run into often. Assumes Python 3.

Index

  1. Import
  2. Templates
    1. Single plot
    2. Multiple Plots
  3. Chart Types
    1. Line
    2. Vertical Line
  4. Properties
    1. Dimensions
    2. Title

Import

import matplotlib.pyplot as plt

Templates

Single plot

fig, ax =  plt.subplots(figsize=(18, 5))

Multiple Plots

One row, two columns:

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(18, 5))

Chart Types

Line

ax.plot(xs, ys)

Vertical Line

ax.plot(x, color='red')

Properties

Dimensions

Do on creation, via figsize:

fig, ax =  plt.subplots(figsize=(18, 5))

Width 18 and height 5 are assumed to be inches.

Title

ax.set_title('My title')