Python - Matplotlib Cheatsheets
Python - Matplotlib Cheatsheets
756 plot([X],Y,[fmt],…) API ax.set_[xy]scale(scale,…) from matplotlib import ticker import [Link] as mpla
432 X, Y, fmt, color, marker, linestyle linear
0.0 log
0.0
ax.[xy]axis.set_[minor|major]_locator(locator)
1 - + any values
2.5 0 +
values > 0
2.5 2 1 0logit
[Link]() T = [Link](0,2*[Link],100)
Quick start 765 1234567 X,scatter(X,Y,…) API
Y, [s]izes, [c]olors, marker, cmap 0.0
API 2 0 2 symlog0.0 101010 0 < values < 1 [Link](0.5)
S = [Link](T)
line, = [Link](T, S)
423 -
2.5
+
any values
2.5 0 1
0.0 0.5 1.0
[Link]([0, 1, 5])
1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
def animate(i):
1
import numpy as np
0 1 5 line.set_ydata([Link](T+i/50))
import matplotlib as mpl
import [Link] as plt 756 1234567 x,bar[h](x,height,…) API 10 00100
Projections
1
2
[Link](numticks=3)
0.0 2.5 5.0
anim = [Link](
3421
[Link](n=4)
Z, cmap, interpolation, extent, origin 0.0 1.5 3.0 4.5
fig, ax = [Link]() [Link](base=10, numticks=15) [Link](style)
[Link](“[Link]”)
Tick formatters
0.0 0.0 0.0
linestyle or ls
0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6
4 API
Title Blue signal [Link](['', '0', '1', ...]) bmh Solarize_Light2 seaborn-notebook
Major tick
432 Red signal Z, explode, labels, colors, radius "-" ":" "--" "-." (0,(0.01,2)) 0 0.25 0.50 1 0.75 0.25 2 0.50 0.75 3 0.25 0.50 0.75 4 0.25 0.50 5 1.0
0.5
1.0
0.5
1.0
0.5
1
Legend
capstyle or dash_capstyle [Link](lambda x, pos: "[%.2f]" % x) 0.0 0.0 0.0
2
432 Y1, Y2, color, where 0.0 1.0 2.0 3.0 4.0 5.0 [Link].set_alpha(0)
Y axis label Markers 1 '.' 'o' 's' 'P' 'X' '*' 'p' 'D' '<' '>' '^' 'v'
0%
[Link](xmax=5)
20% 40% 60% 80% 100%
ax.set_[xy]lim(vmin, vmax)
ax.set_[xy]label(label)
1
(scatter plot)
1234567
Advanced plots
'1' '2' '3' '4' '+' 'x' '|' '_' 4 5 6 7 ax.set_[xy]ticks(list)
ax.set_[xy]ticklabels(list)
Ornaments
765
'$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $' ax.set_[sup]title(title)
step(X,Y,[fmt],…) API markevery ax.tick_params(width=10, …)
432
Spines [Link](…) API
Figure Line X, Y, fmt, color, marker, where
Axes (line plot) 10 [0, -1] (25, 5) [0, 25, -1] handles, labels, loc, title, frameon ax.set_axis_[on|off]()
0 1
0 0.25 0.50 0.75 1 1.25 1.50 1.75 2 2.25 2.50 2.75 3 3.25 3.50 3.75 4
765 1234567 X,boxplot(X,…)
title
fig.tight_layout()
X axis label API
Colors Legend
432
Minor tick label API [Link](), [Link]()
X axis label notch, sym, bootstrap, widths handletextpad
label
handle
markerfacecolor (mfc) [Link](’axes’, linewidth=1, …)
1
1 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 handlelength
’Cn’
Subplots layout 0 Label 1 Label 3 [Link].set_alpha(0)
API
765 246 X,errorbar(X,Y,xerr,yerr,…) API
0 b 2 g 4 r 6 c 8 m 10 y 12 k 14 w 16 ’x’
1 DarkRed Firebrick Crimson IndianRed Salmon labelspacing markeredgecolor (mec) text=r’$\frac{-e^{i\pi}}{2^n}$’
42
’name’
Y, xerr, yerr, fmt 10 (1,0,0) (1,0,0,0.75) (1,0,0,0.5) (1,0,0,0.25) Label 2 Label 4
fig, axs = [Link](3,3) 3
subplot[s](rows,cols,…) API 0 2 4 6 8 10 12 14 16 (R,G,B[,A])
10 #FF0000
1
borderpad
0 2 4 #FF0000BB
6 8 #FF000088
10 12 #FF000044
14 16 ’#RRGGBB[AA]’
10 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
columnspacing numpoints or scatterpoints
Keyboard shortcuts API
71
0 2 4 6 8 10 12 14 16 ’x.y’
1234567 X,hist(X,
borderaxespad
61
0
51
bins, …)
API 0 2 4 6 8 10 12 14 16 ctrl + s Save ctrl + w Close plot
G = gridspec(rows,cols,…) 41
31
21
API bins, range, density, weights [Link](…) API r Reset view f Fullscreen 0/1
ax = G[0,:]
111 Colormaps API
mappable, ax, cax, orientation
f View forward b View back
765 1234567 violinplot(D,…) API plt.get_cmap(name) p Pan view o Zoom to rect
ax.inset_axes(extent) API
423 D, positions, widths, vert
Uniform
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x X pan/zoom y Y pan/zoom
1 viridis g Minor grid 0/1 G Major grid 0/1
756 1234567 barbs([X],[Y], API U, V, …) magma [Link](…) API
l X axis log/linear L Y axis log/linear
d=make_axes_locatable(ax)
ax=d.new_horizontal(’10%’)
API
4
321 X, Y, U, V, C, length, pivot, sizes
Sequential
plasma
text, xy, xytext, xycoords, textcoords, arrowprops
A 2 9 1 I
grey lemonchiffon skyblue
inferno
darkgray khaki lightskyblue → [Link](”[Link]”)
darkgrey palegoldenrod steelblue
magma silver darkkhaki aliceblue … save a transparent figure?
top lightgray ivory dodgerblue
axes width cividis lightgrey beige lightslategray → [Link](”[Link]”, transparent=True)
gainsboro lightyellow lightslategrey
whitesmoke lightgoldenrodyellow slategray … clear a figure?
w olive slategrey
→ [Link]()
Sequential colormaps white
snow
y
yellow
lightsteelblue
cornflowerblue
rosybrown olivedrab royalblue … close all figures?
B 6 10 7 H
figure height
lightcoral yellowgreen ghostwhite
axes height indianred darkolivegreen lavender → [Link](”all”)
brown greenyellow midnightblue
Greys
firebrick chartreuse navy … remove ticks?
hspace Purples maroon lawngreen darkblue → ax.set_xticks([])
darkred honeydew mediumblue
Blues r darkseagreen b … remove tick labels ?
red palegreen blue
Greens mistyrose lightgreen slateblue → ax.set_[xy]ticklabels([])
salmon forestgreen darkslateblue
tomato limegreen mediumslateblue … rotate tick labels ?
C 3 8 4 G
left bottom wspace right Oranges
darksalmon darkgreen mediumpurple
Reds coral g rebeccapurple → ax.set_[xy]ticks(rotation=90)
orangered green blueviolet
YlOrBr lightsalmon lime indigo … hide top spine?
sienna seagreen darkorchid
figure width
D E F
YlOrRd seashell mediumseagreen darkviolet → [Link][’top’].set_visible(False)
chocolate springgreen mediumorchid
OrRd saddlebrown mintcream thistle … hide legend border?
sandybrown mediumspringgreen plum
peachpuff mediumaquamarine violet → [Link](frameon=False)
PuRd peru aquamarine purple
Extent & origin API linen turquoise darkmagenta [Link](loc=”string”, bbox_to_anchor=(x,y)) … show error as shaded region?
RdPu bisque lightseagreen m
darkorange mediumturquoise fuchsia
2: upper left 9: upper center 1: upper right → ax.fill_between(X, Y+error, Y‐error)
[Link]( extent=…, origin=… ) BuPu burlywood azure magenta
GnBu
antiquewhite
tan
lightcyan
paleturquoise
orchid
mediumvioletred 6: center left 10: center 7: center right … draw a rectangle?
origin="upper" origin="upper" PuBu
navajowhite
blanchedalmond
darkslategray
darkslategrey
deeppink
hotpink 3: lower left 8: lower center 4: lower right → ax.add_patch([Link]((0, 0),1,1)
5
(0,0) (0,0)
YlGnBu
papayawhip
moccasin
teal
darkcyan
lavenderblush
palevioletred … draw a vertical line?
orange c crimson A: upper right / (-0.1,0.9) B: center right / (-0.1,0.5) → [Link](x=0.5)
PuBuGn wheat aqua pink
oldlace cyan lightpink C: lower right / (-0.1,0.1) D: upper left / (0.1,-0.1) … draw outside frame?
BuGn E: upper center / (0.5,-0.1) F: upper right / (0.9,-0.1)
0
(4,4) (4,4) → [Link](…, clip_on=False)
extent=[0,10,0,5] extent=[10,0,0,5] YlGn G: lower left / (1.1,0.1) H: center left / (1.1,0.5)
Image interpolation API … use transparency?
origin="lower" origin="lower" I: upper left / (1.1,0.9) J: lower right / (0.9,1.1) → [Link](…, alpha=0.25)
5
(4,4) (4,4)
Diverging colormaps K: lower center / (0.5,1.1) L: lower left / (0.1,1.1) … convert an RGB image into a gray image?
→ gray = 0.2989*R+0.5870*G+0.1140*B
PiYG Annotation connection styles API … set figure background color?
0
(0,0) (0,0) → [Link].set_facecolor(“grey”)
extent=[0,10,0,5] extent=[10,0,0,5] PRGn
0 10 0 10 BrBG
arc3, arc3, angle3, … get a reversed colormap?
None none nearest
rad=0 rad=0.3 angleA=0,
angleB=90
→ plt.get_cmap(“viridis_r”)
PuOr
… get a discrete colormap?
Text alignments
RdGy
API → plt.get_cmap(“viridis”, 10)
RdBu
… show a figure for one second?
[Link]( …, ha=… , va=…, … ) RdYlBu
→ [Link](block=False), [Link](1)
Matplotlib
RdYlGn
(1,1) angle, angle, arc,
top
Performance tips
Spectral angleA=-90, angleA=-90, angleA=-90,
Text parameters
fraction=0.3 fraction=-0.3 angle=180,
API fraction=-0.2 cla(), imshow(…), [Link]() slow
Pastel1 spline36 hanning hamming im.set_data(…), [Link]() fast
[Link]( …, family=… , size=…, weight = …) Pastel2
[Link]( …, fontproperties = … ) Paired Beyond Matplotlib
The quick brown fox
Accent
xx-large (1.73)
Dark2 Seaborn: Statistical Data Visualization
The quick brown fox x-large (1.44)
Set1 Cartopy: Geospatial Data Processing
The quick brown fox large (1.20) Annotation arrow styles API yt: Volumetric data Visualization
The quick brown fox
The quick brown fox
medium
small
(1.00)
(0.83)
Set2
Set3
hermite kaiser quadric mpld3: Bringing Matplotlib to the browser
The quick brown fox x-small (0.69) Datashader: Large data processing pipeline
The quick brown fox xx-small (0.58) tab10
- <- -> plotnine: A Grammar of Graphics for Python
tab20
The quick brown fox jumps over the lazy dog black (900)
The quick brown fox jumps over the lazy dog bold (700) tab20b
The quick brown fox jumps over the lazy dog semibold (600) tab20c <-> <|- -|>
The quick brown fox jumps over the lazy dog normal (400) Matplotlib Cheatsheets
The quick brown fox jumps over the lazy dog Copyright (c) 2021 Matplotlib Development Team
ultralight (100)
Miscellaneous colormaps
catrom gaussian bessel Released under a CC‐BY 4.0 International License
The quick brown fox jumps over the lazy dog monospace
<|-|> ]-[ ]-
The quick brown fox jumps over the lazy dog serif
The quick brown fox jumps over the lazy dog sans
The quick brown fox jumps over the lazy dog cursive
terrain
The quick brown fox jumps over the lazy dog italic
ocean -[ |-| simple
The quick brown fox jumps over the lazy dog normal cubehelix
rainbow
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
small-caps
normal twilight mitchell sinc lanczos fancy wedge