forked from matplotlib/cheatsheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handout-intermediate.tex
207 lines (174 loc) · 7.12 KB
/
handout-intermediate.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
\documentclass[10pt,landscape,a4paper]{article}
\usepackage[right=10mm, left=10mm, top=10mm, bottom=10mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[rm,light]{roboto}
\usepackage{xcolor}
\usepackage{graphicx}
\graphicspath{{./figures/}}
\usepackage{multicol}
\usepackage{colortbl}
\usepackage{array}
\setlength\parindent{0pt}
\setlength{\tabcolsep}{2pt}
\baselineskip=0pt
\setlength\columnsep{1em}
\definecolor{Gray}{gray}{0.85}
% --- Listing -----------------------------------------------------------------
\usepackage{listings}
\lstset{
frame=tb, framesep=4pt, framerule=0pt,
backgroundcolor=\color{black!5},
basicstyle=\ttfamily,
commentstyle=\ttfamily\color{black!50},
breakatwhitespace=false,
breaklines=true,
extendedchars=true,
keepspaces=true,
language=Python,
rulecolor=\color{black},
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2,
%
emph = { plot, scatter, imshow, bar, contourf, pie, subplots, spines,
add_gridspec, add_subplot, set_xscale, set_minor_locator,
annotate, set_minor_formatter, tick_params, fill_betweenx, text, legend,
errorbar, boxplot, hist, title, xlabel, ylabel, suptitle },
emphstyle = {\ttfamily\bfseries}
}
% --- Fonts -------------------------------------------------------------------
\usepackage{fontspec}
\usepackage[babel=true]{microtype}
\defaultfontfeatures{Ligatures = TeX, Mapping = tex-text}
\setsansfont{Roboto} [ Path = fonts/roboto/Roboto-,
Extension = .ttf,
UprightFont = Light,
ItalicFont = LightItalic,
BoldFont = Regular,
BoldItalicFont = Italic ]
\setromanfont{RobotoSlab} [ Path = fonts/roboto-slab/RobotoSlab-,
Extension = .ttf,
UprightFont = Light,
BoldFont = Bold ]
\setmonofont{RobotoMono} [ Path = fonts/roboto-mono/RobotoMono-,
Extension = .ttf,
Scale = 0.90,
UprightFont = Light,
ItalicFont = LightItalic,
BoldFont = Regular,
BoldItalicFont = Italic ]
\renewcommand{\familydefault}{\sfdefault}
% -----------------------------------------------------------------------------
\begin{document}
\thispagestyle{empty}
\section*{\LARGE \rmfamily
Matplotlib \textcolor{orange}{\mdseries for intermediate users}}
\begin{multicols*}{3}
A matplotlib figure is composed of a hierarchy of elements that forms
the actual figure. Each element can be modified. \medskip
\includegraphics[width=\linewidth]{anatomy-cropped.pdf}
\subsection*{\rmfamily Figure, axes \& spines}
% -----------------------------------------------------------------------------
\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}}
\begin{lstlisting}[belowskip=-\baselineskip]
fig, axs = plt.subplots((3,3))
axs[0,0].set_facecolor("#ddddff")
axs[2,2].set_facecolor("#ffffdd")
\end{lstlisting}
& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{layout-subplot-color.pdf}}
\end{tabular}
% -----------------------------------------------------------------------------
\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}}
\begin{lstlisting}[belowskip=-\baselineskip]
gs = fig.add_gridspec(3, 3)
ax = fig.add_subplot(gs[0, :])
ax.set_facecolor("#ddddff")
\end{lstlisting}
& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{layout-gridspec-color.pdf}}
\end{tabular}
% -----------------------------------------------------------------------------
\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}}
\begin{lstlisting}[belowskip=-\baselineskip]
fig, ax = plt.subplots()
ax.spines["top"].set_color("None")
ax.spines["right"].set_color("None")
\end{lstlisting}
& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{layout-spines.pdf}}
\end{tabular}
% -----------------------------------------------------------------------------
\subsection*{\rmfamily Ticks \& labels}
\begin{lstlisting}[basicstyle=\ttfamily\small]
from mpl.ticker import MultipleLocator as ML
from mpl.ticker import ScalarFormatter as SF
ax.xaxis.set_minor_locator(ML(0.2))
ax.xaxis.set_minor_formatter(SF())
ax.tick_params(axis='x',which='minor',rotation=90)
\end{lstlisting}
\includegraphics[width=\linewidth]{tick-multiple-locator.pdf}
% -----------------------------------------------------------------------------
\subsection*{\rmfamily Lines \& markers}
\begin{lstlisting}
X = np.linspace(0.1, 10*np.pi, 1000)
Y = np.sin(X)
ax.plot(X, Y, "C1o:", markevery=25, mec="1.0")
\end{lstlisting}
\includegraphics[width=\linewidth]{sine-marker.pdf}
% -----------------------------------------------------------------------------
\subsection*{\rmfamily Scales \& projections}
\begin{lstlisting}
fig, ax = plt.subplots()
ax.set_xscale("log")
ax.plot(X, Y, "C1o-", markevery=25, mec="1.0")
\end{lstlisting}
\includegraphics[width=\linewidth]{sine-logscale.pdf}
\subsection*{\rmfamily Text \& ornaments}
\begin{lstlisting}[]
ax.fill_betweenx([-1,1],[0],[2*np.pi])
ax.text(0, -1, r" Period $\Phi$")
\end{lstlisting}
\includegraphics[width=\linewidth]{sine-period.pdf}
% -----------------------------------------------------------------------------
\subsection*{\rmfamily Legend}
\begin{lstlisting}[]
ax.plot(X, np.sin(X), "C0", label="Sine")
ax.plot(X, np.cos(X), "C1", label="Cosine")
ax.legend(bbox_to_anchor=(0,1,1,.1),ncol=2,
mode="expand", loc="lower left")
\end{lstlisting}
\includegraphics[width=\linewidth]{sine-legend.pdf}
% -----------------------------------------------------------------------------
\subsection*{\rmfamily Annotation}
\begin{lstlisting}[]
ax.annotate("A", (X[250],Y[250]),(X[250],-1),
ha="center", va="center",arrowprops =
{"arrowstyle" : "->", "color": "C1"})
\end{lstlisting}
\includegraphics[width=\linewidth]{sine-annotate.pdf}
% -----------------------------------------------------------------------------
\subsection*{\rmfamily Colors}
Any color can be used, but Matplotlib offers sets of colors:\\
\includegraphics[width=\linewidth]{colors-cycle.pdf} \smallskip
\includegraphics[width=\linewidth]{colors-grey.pdf}\\
%As well as nice colormaps (viridis an magma):\\
%\includegraphics[width=\linewidth]{colormap-viridis.pdf} \smallskip
%\includegraphics[width=\linewidth]{colormap-magma.pdf} \medskip
% -----------------------------------------------------------------------------
\subsection*{\rmfamily Size \& DPI}
Consider a square figure to be included in a two-columns A4 paper with
2cm margins on each side and a column separation of 1cm. The width of
a figure is (21 - 2*2 - 1)/2 = 8cm. One inch being 2.54cm, figure size
should be 3.15$\times$3.15 in.
\begin{lstlisting}[]
fig = plt.figure(figsize=(3.15,3.15), dpi=50)
plt.savefig("figure.pdf", dpi=600)
\end{lstlisting}
\vfill
%
{\scriptsize Matplotlib 3.2 handout for intermediate users. Copyright
(c) 2020 Nicolas P. Rougier. Released under a CC-BY 4.0
License. Supported by NumFocus Grant \#12345.\par}
\end{multicols*}
\end{document}