Anatomy of A Figure: Cheat Sheet
Anatomy of A Figure: Cheat Sheet
P. ROUGIER
756
DESIGNED BY NICOLAS
plot([X],Y,[fmt],…) API ax.set_[xy]scale(scale,…) from matplotlib import ticker import matplotlib.animation as mpla
Cheat sheet
432 Version 3.2 X, Y, fmt, color, marker, linestyle
0.0 linear
0.0 log
ax.[xy]axis.set_[minor|major]_locator(locator)
0.5
0.0
0.5
0.0
0.5
fig.show()
765 1234567 quiver([X],[Y],U,V,…)
from matplotlib import ticker 1.0
0 1 2 3 4 5 6
1.0
0 1 2 3 4 5 6 7
1.0
0 1 2 3 4 5 6
ax.[xy]axis.set_[minor|major]_formatter(formatter)
API ggplot seaborn fast
API 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
bmh Solarize_Light2 seaborn-notebook
Anatomy of a figure
4 432 Z, explode, labels, colors, radius "-" ":" "--" "-." (0,(0.01,2)) ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)
1.0
0.5
1.0
0.5
1.0
0.5
1
Title
Major tick
Blue signal
Red signal capstyle or dash_capstyle [0.00] [1.00] [2.00] [3.00] [4.00] [5.00] 0.0
0.5
0.0
0.5
0.0
0.5
text(x,y,text,…) API "butt" "round" "projecting" >0< >1< >2< >3< >4< >5<
432 TEX
Minor tick
ticker.ScalarFormatter()
3
Quick reminder
1
0 1 2 3 4 5
Major tick label Grid
Markers ticker.StrMethodFormatter('{x}')
1 '.' 'o' 's' 'P' 'X' '*' 'p' 'D' '<' '>' '^' 'v' ax.set_[xy]lim(vmin, vmax)
ax.set_[xy]label(label)
1234567
Y axis label Markers
Ornaments
(scatter plot)
'1' '2' '3' '4' '+' 'x' '|' '_' 4 5 6 7 ax.set_[xy]ticks(list)
1 Advanced plots ax.set_[xy]ticklabels(list)
ax.legend(…)
765
'$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $''$ $' API ax.set_[sup]title(title)
Spines step(X,Y,[fmt],…) API markevery handles, labels, loc, title, frameon ax.tick_params(width=10, …)
Figure
Axes
Line
(line plot)
432 X, Y, fmt, color, marker, where 10 [0, -1] (25, 5) [0, 25, -1] ax.set_axis_[on|off]()
1
title
0
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 Legend
765 1234567 X,boxplot(X,…)
label
Minor tick label
X axis label handletextpad handle
markerfacecolor (mfc)
ax.tight_layout()
X axis label API
Colors
432
handlelength
API plt.gcf(), plt.gca()
notch, sym, bootstrap, widths Label 1 Label 3 mpl.rc(’axes’, linewidth=1, …)
1
1 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 labelspacing markeredgecolor (mec)
Subplots layout
’Cn’ fig.patch.set_alpha(0)
0
Label 2 Label 4
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’ text=r’$\frac{-e^{i\pi}}{2^n}$’
API 1 DarkRed Firebrick Crimson IndianRed Salmon borderpad
71
61
51 1234567 X,hist(X, bins, …) API
0
0
0
2
2
4
4
6
6
8
8
10 12 14 16 ’x.y’
10 12 14 16 ax.colorbar(…) API
ctrl + s Save ctrl + w Close plot
G = gridspec(rows,cols,…)
41
31
21
API bins, range, density, weights mappable, ax, cax, orientation
Reset view Fullscreen 0/1
111 Colormaps
ax = G[0,:] r f
API
View forward b View back
765 1234567 violinplot(D,…)
f
API plt.get_cmap(name) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
Pan view o Zoom to rect
423
p
ax.inset_axes(extent) API D, positions, widths, vert
Uniform X pan/zoom y Y pan/zoom
1
x
viridis ax.annotate(…) Minor grid 0/1 G Major grid 0/1
756 1234567 barbs([X],[Y],
API g
U, V, …) API magma text, xy, xytext, xycoords, textcoords, arrowprops
X axis log/linear L Y axis log/linear
4
l
X, Y, U, V, C, length, pivot, sizes
321
d=make_axes_locatable(ax) API plasma
ax=d.new_horizontal(’10%’) Sequential Annotation
Ten simple rules
765 1234567 eventplot(positions,…) xytext xy
Greys READ
API YlOrBr textcoords ycoords
plt.subplot_adjust( … )
L K J
black floralwhite darkturquoise
viridis k darkgoldenrod cadetblue
dimgray goldenrod powderblue
plasma dimgrey cornsilk lightblue
gray gold deepskyblue - <- ->
grey lemonchiffon skyblue
A 7 8 9 I
inferno darkgray khaki lightskyblue
magma darkgrey palegoldenrod steelblue
silver darkkhaki aliceblue
lightgray ivory dodgerblue <-> <|- -|>
top axes width
cividis lightgrey beige lightslategray
gainsboro lightyellow lightslategrey
whitesmoke lightgoldenrodyellow slategray
w olive slategrey <|-|> ]-[ ]-
white y lightsteelblue
Sequential colormaps snow
rosybrown
yellow
olivedrab
cornflowerblue
royalblue
lightcoral yellowgreen ghostwhite
B 4 5 6 H
figure height
indianred darkolivegreen lavender -[ |-| simple
axes height brown greenyellow midnightblue
Greys firebrick chartreuse navy
maroon lawngreen darkblue
hspace Purples darkred honeydew mediumblue fancy wedge
r darkseagreen b
Blues red palegreen blue
mistyrose lightgreen slateblue
Greens salmon forestgreen darkslateblue
tomato limegreen mediumslateblue
darksalmon darkgreen mediumpurple
bottom coral g rebeccapurple
C 1 2 3 G
left wspace right Oranges
orangered green blueviolet
Reds lightsalmon lime indigo
YlOrBr
sienna
seashell
seagreen
mediumseagreen
darkorchid
darkviolet
How do I …
chocolate springgreen mediumorchid
D E F
figure width
YlOrRd saddlebrown
sandybrown
mintcream
mediumspringgreen
thistle
plum … resize a figure?
OrRd peachpuff
peru
mediumaquamarine
aquamarine
violet
purple → fig.set_size_inches(w,h)
linen turquoise darkmagenta … save a figure?
Extent & origin API
PuRd bisque
darkorange
lightseagreen
mediumturquoise
m
fuchsia ax.legend(loc=”string”, bbox_to_anchor=(x,y))
RdPu burlywood azure magenta → fig.savefig(”figure.pdf”)
antiquewhite lightcyan orchid
ax.imshow( extent=…, origin=… ) BuPu tan paleturquoise mediumvioletred 1: lower left 2: lower center 3: lower right … save a transparent figure?
navajowhite darkslategray deeppink
GnBu blanchedalmond darkslategrey hotpink 4: center left 5: center 6: center right → fig.savefig(”figure.pdf”, transparent=True)
papayawhip teal lavenderblush
origin="upper" origin="upper" PuBu
moccasin
orange
darkcyan
c
palevioletred
crimson 7: upper left 8: upper center 9: upper right … clear a figure?
5
(0,0) (0,0) wheat
oldlace
aqua
cyan
pink
lightpink
→ ax.clear()
YlGnBu
A: upper right / B: center right / … close all figures?
PuBuGn
(-.1,.9) (-.1,.5) → plt.close(”all”)
(4,4) (4,4)
BuGn C: lower right / D: upper left / (.1,-.1) … remove ticks?
0 YlGn
extent=[0,10,0,5] extent=[10,0,0,5] (-.1,.1) → ax.set_xticks([])
origin="lower" origin="lower" E: upper center / F: upper right / … remove tick labels ?
5
(4,4) (4,4) Image interpolation
Diverging colormaps (.5,-.1) (.9,-.1) → ax.set_[xy]ticklabels([])
API
G: lower left / (1.1,.1) H: center left / … rotate tick labels ?
(1.1,.5) → ax.set_[xy]ticks(rotation=90)
PiYG
0
(0,0) (0,0) I: upper left / (1.1,.9) J: lower right / … hide top spine?
extent=[0,10,0,5] extent=[10,0,0,5] PRGn
0 10 0 10 (.9,1.1) → ax.spines[’top’].set_visible(False)
BrBG
K: lower center / L: lower left / (.1,1.1) … hide legend border?
PuOr (.5,1.1) → ax.legend(frameon=False)
Text alignments API RdGy None none nearest … show error as shaded region?
RdBu
ax.text( …, ha=… , va=…, … ) → ax.fill_between(X, Y+error, Y‐error)
RdYlBu
… draw a rectangle?
→ ax.add_patch(plt.Rectangle((0, 0),1,1)
Matplotlib
RdYlGn
(1,1)
Spectral
… draw a vertical line?
top coolwarm → ax.axvline(x=0.5)
center bwr bilinear bicubic spline16 Annotation connection styles API
… draw outside frame?
baseline seismic → ax.plot(…, clip_on=False)
bottom
(0,0) … use transparency?
left center right arc3, arc3, angle3,
rad=0 rad=0.3 angleA=0,
→ ax.plot(…, alpha=0.25)
Qualitative colormaps angleB=90