Show Menu
Cheatography

Matplotlib Cheat Sheet (DRAFT) by

A definitive cheatsheet/reference guide for Python's matplotlib library.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Line Properties

color
/
c
Line color (name, hex, RGB tuple)
linewidth
/
lw
Thickness of the line
linestyle
/
ls
Line pattern (ie,
"­-"
,
"­--"
,
"­-."
,
"­:"
)
dashes
Custom dash pattern as a list of on/off lengths
zorder
Draw order; higher values appear on top
label
Name used in legend

Marker Appearance

marker
Shape of each plotted point/­symbol Docs
markersize
/
ms
Size of the marker
marker­edg­ecolor
/
mec
Outline color of the marker
marker­edg­ewidth
/
mew
Thickness of the marker’s edge
marker­fac­ecolor
/
mfc
Fill color of the marker
marker­fac­eco­loralt
Alternate fill color (for special fill styles)
fillstyle
How marker is filled (
"­ful­l"
,
"­lef­t"
,
"­non­e"
)
alpha
Transp­arency
 

plt.p­lot()


import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 10, 0.5)
y = 3(x2) + 5x

fig, ax = plt.subplots()
ax.plot(x, y,
        color='navy',
        linewidth=2,
        linestyle='--',
        marker='o',
        markersize=4,
        alpha=0.9,
        label='Signal')

ax.set(
    title="Line Plot",
    xlabel="Time",
    ylabel="Value"
)
ax.legend()
Plot points and connect them with a line

Used for continuous functions like time series and trends

Lineplot

 

plt.s­cat­ter()


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 8, 7]
sizes = [20, 50, 100, 200, 500]
colors = [10, 20, 30, 40, 50]

plt.scatter(
    x, y,
    s=sizes,
    c=colors,
    cmap='viridis'
)
plt.title('Hi')
plt.xlabel('X')
plt.ylabel('Y')
plt.colorbar()
plt.show()
Plot discrete points

Scatte­rplot