|
| 1 | +\documentclass[10pt,landscape,a4paper]{article} |
| 2 | +\usepackage[right=10mm, left=10mm, top=10mm, bottom=10mm]{geometry} |
| 3 | +\usepackage[utf8]{inputenc} |
| 4 | +\usepackage[T1]{fontenc} |
| 5 | +\usepackage[english]{babel} |
| 6 | +\usepackage[rm,light]{roboto} |
| 7 | +\usepackage{xcolor} |
| 8 | +\usepackage{graphicx} |
| 9 | +\graphicspath{{./figures/}} |
| 10 | +\usepackage{multicol} |
| 11 | +\usepackage{colortbl} |
| 12 | +\usepackage{array} |
| 13 | +\setlength\parindent{0pt} |
| 14 | +\setlength{\tabcolsep}{2pt} |
| 15 | +\baselineskip=0pt |
| 16 | +\setlength\columnsep{1em} |
| 17 | +\definecolor{Gray}{gray}{0.85} |
| 18 | + |
| 19 | +% --- Listing ----------------------------------------------------------------- |
| 20 | +\usepackage{listings} |
| 21 | +\lstset{ |
| 22 | + frame=tb, framesep=4pt, framerule=0pt, |
| 23 | + backgroundcolor=\color{black!5}, |
| 24 | + basicstyle=\ttfamily, |
| 25 | + commentstyle=\ttfamily\color{black!50}, |
| 26 | + breakatwhitespace=false, |
| 27 | + breaklines=true, |
| 28 | + extendedchars=true, |
| 29 | + keepspaces=true, |
| 30 | + language=Python, |
| 31 | + rulecolor=\color{black}, |
| 32 | + showspaces=false, |
| 33 | + showstringspaces=false, |
| 34 | + showtabs=false, |
| 35 | + tabsize=2, |
| 36 | + % |
| 37 | + emph = { plot, scatter, imshow, bar, contourf, pie, |
| 38 | + errorbar, boxplot, hist, title, xlabel, ylabel, suptitle }, |
| 39 | + emphstyle = {\ttfamily\bfseries} |
| 40 | +} |
| 41 | + |
| 42 | +% --- Fonts ------------------------------------------------------------------- |
| 43 | +\usepackage{fontspec} |
| 44 | +\usepackage[babel=true]{microtype} |
| 45 | +\defaultfontfeatures{Ligatures = TeX, Mapping = tex-text} |
| 46 | +\setsansfont{Roboto} [ Path = fonts/roboto/Roboto-, |
| 47 | + Extension = .ttf, |
| 48 | + UprightFont = Light, |
| 49 | + ItalicFont = LightItalic, |
| 50 | + BoldFont = Regular, |
| 51 | + BoldItalicFont = Italic ] |
| 52 | +\setromanfont{RobotoSlab} [ Path = fonts/roboto-slab/RobotoSlab-, |
| 53 | + Extension = .ttf, |
| 54 | + UprightFont = Light, |
| 55 | + BoldFont = Bold ] |
| 56 | +\setmonofont{RobotoMono} [ Path = fonts/roboto-mono/RobotoMono-, |
| 57 | + Extension = .ttf, |
| 58 | + Scale = 0.90, |
| 59 | + UprightFont = Light, |
| 60 | + ItalicFont = LightItalic, |
| 61 | + BoldFont = Regular, |
| 62 | + BoldItalicFont = Italic ] |
| 63 | +\renewcommand{\familydefault}{\sfdefault} |
| 64 | + |
| 65 | +% ----------------------------------------------------------------------------- |
| 66 | +\begin{document} |
| 67 | +\thispagestyle{empty} |
| 68 | + |
| 69 | +\section*{\LARGE \rmfamily |
| 70 | + Matplotlib \textcolor{orange}{\mdseries for beginners}} |
| 71 | + |
| 72 | +\begin{multicols*}{3} |
| 73 | + |
| 74 | +Matplotlib is a library for making 2D plots in Python. It is designed |
| 75 | +with the philosophy that you should be able to create simple plots |
| 76 | +with just a few commands:\\ |
| 77 | + |
| 78 | +\fbox{1} \textbf{Initialize} |
| 79 | +\begin{lstlisting} |
| 80 | + import numpy as np |
| 81 | + import matplotlib.pyplot as plt |
| 82 | +\end{lstlisting} |
| 83 | +% |
| 84 | +\fbox{2} \textbf{Prepare} |
| 85 | +\begin{lstlisting} |
| 86 | + X = np.linspace(0, 4*np.pi, 1000) |
| 87 | + Y = np.sin(X) |
| 88 | +\end{lstlisting} |
| 89 | +% |
| 90 | +\fbox{3} \textbf{Render} |
| 91 | +\begin{lstlisting} |
| 92 | + plt.plot(X, Y) |
| 93 | + plt.show() |
| 94 | +\end{lstlisting} |
| 95 | +% |
| 96 | +\fbox{4} \textbf{Observe} \medskip\\ |
| 97 | +\includegraphics[width=\linewidth]{sine.pdf} |
| 98 | + |
| 99 | +% ----------------------------------------------------------------------------- |
| 100 | +\subsection*{\rmfamily Choose} |
| 101 | +% ----------------------------------------------------------------------------- |
| 102 | + |
| 103 | +Matplotlib offers several kind of plots (see Gallery): \medskip |
| 104 | + |
| 105 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 106 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 107 | + X = np.random.uniform(0, 1, 100) |
| 108 | + Y = np.random.uniform(0, 1, 100) |
| 109 | + plt.scatter(X, Y) |
| 110 | +\end{lstlisting} |
| 111 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{basic-scatter.pdf}} |
| 112 | +\end{tabular} |
| 113 | +% ----------------------------------------------------------------------------- |
| 114 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 115 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 116 | + X = np.arange(10) |
| 117 | + Y = np.random.uniform(1, 10, 10) |
| 118 | + plt.bar(X, Y) |
| 119 | +\end{lstlisting} |
| 120 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{basic-bar.pdf}} |
| 121 | +\end{tabular} |
| 122 | +% ----------------------------------------------------------------------------- |
| 123 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 124 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 125 | + Z = np.random.uniform(0, 1, (8,8) |
| 126 | + |
| 127 | + plt.imshow(Z) |
| 128 | +\end{lstlisting} |
| 129 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{basic-imshow.pdf}} |
| 130 | +\end{tabular} |
| 131 | +% ----------------------------------------------------------------------------- |
| 132 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 133 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 134 | + Z = np.random.uniform(0, 1, (8,8) |
| 135 | + |
| 136 | + plt.contourf(Z) |
| 137 | +\end{lstlisting} |
| 138 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{basic-contour.pdf}} |
| 139 | +\end{tabular} |
| 140 | +% ----------------------------------------------------------------------------- |
| 141 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 142 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 143 | + Z = np.random.uniform(0, 1, 4) |
| 144 | + |
| 145 | + plt.pie(Z) |
| 146 | +\end{lstlisting} |
| 147 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{basic-pie.pdf}} |
| 148 | +\end{tabular} |
| 149 | +% ----------------------------------------------------------------------------- |
| 150 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 151 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 152 | + Z = np.random.normal(0, 1, 100) |
| 153 | + |
| 154 | + plt.hist(Z) |
| 155 | +\end{lstlisting} |
| 156 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{advanced-hist.pdf}} |
| 157 | +\end{tabular} |
| 158 | +% ----------------------------------------------------------------------------- |
| 159 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 160 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 161 | + X = np.arange(5) |
| 162 | + Y = np.random.uniform(0,1,5) |
| 163 | + plt.errorbar(X, Y, Y/4) |
| 164 | +\end{lstlisting} |
| 165 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{advanced-errorbar.pdf}} |
| 166 | +\end{tabular} |
| 167 | +% ----------------------------------------------------------------------------- |
| 168 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 169 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 170 | + Z = np.random.normal(0,1,(100,3)) |
| 171 | + |
| 172 | + plt.boxplot(Z) |
| 173 | +\end{lstlisting} |
| 174 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{advanced-boxplot.pdf}} |
| 175 | +\end{tabular} |
| 176 | + |
| 177 | + |
| 178 | +% ----------------------------------------------------------------------------- |
| 179 | +\subsection*{\rmfamily Tweak} |
| 180 | +% ----------------------------------------------------------------------------- |
| 181 | +You can modify pretty much anything in a plot, including limits, |
| 182 | +colors, markers, line width and styles, ticks and ticks labels, |
| 183 | +titles, etc. \medskip |
| 184 | + |
| 185 | +% ----------------------------------------------------------------------------- |
| 186 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 187 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 188 | + X = np.linspace(0,10,100) |
| 189 | + Y = np.sin(X) |
| 190 | + plt.plot(X, Y, color="black") |
| 191 | +\end{lstlisting} |
| 192 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{plot-color.pdf}} |
| 193 | +\end{tabular} |
| 194 | +% ----------------------------------------------------------------------------- |
| 195 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 196 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 197 | + X = np.linspace(0,10,100) |
| 198 | + Y = np.sin(X) |
| 199 | + plt.plot(X, Y, linestyle="--") |
| 200 | +\end{lstlisting} |
| 201 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{plot-linestyle.pdf}} |
| 202 | +\end{tabular} |
| 203 | +% ----------------------------------------------------------------------------- |
| 204 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 205 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 206 | + X = np.linspace(0,10,100) |
| 207 | + Y = np.sin(X) |
| 208 | + plt.plot(X, Y, linewidth=5) |
| 209 | +\end{lstlisting} |
| 210 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{plot-linewidth.pdf}} |
| 211 | +\end{tabular} |
| 212 | +% ----------------------------------------------------------------------------- |
| 213 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 214 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 215 | + X = np.linspace(0,10,100) |
| 216 | + Y = np.sin(X) |
| 217 | + plt.plot(X, Y, marker="o") |
| 218 | +\end{lstlisting} |
| 219 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{plot-marker.pdf}} |
| 220 | +\end{tabular} |
| 221 | + |
| 222 | + |
| 223 | +% ----------------------------------------------------------------------------- |
| 224 | +\subsection*{\rmfamily Organize} |
| 225 | +% ----------------------------------------------------------------------------- |
| 226 | + |
| 227 | +You can plot several data on the the same figure but you can also |
| 228 | +split a figure in several subplots (named {\em Axes}): \medskip |
| 229 | + |
| 230 | +% ----------------------------------------------------------------------------- |
| 231 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 232 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 233 | + X = np.linspace(0,10,100) |
| 234 | + Y1, Y1 = np.sin(X), np.cos(X) |
| 235 | + plt.plot(X, Y1, Y2) |
| 236 | +\end{lstlisting} |
| 237 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{plot-multi.pdf}} |
| 238 | +\end{tabular} |
| 239 | +% ----------------------------------------------------------------------------- |
| 240 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 241 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 242 | + fig, (ax1, ax2) = plt.subplots((2,1)) |
| 243 | + ax1.plot(X, Y1, color="C1") |
| 244 | + ax2.plot(X, Y2, color="C0") |
| 245 | +\end{lstlisting} |
| 246 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{plot-vsplit.pdf}} |
| 247 | +\end{tabular} |
| 248 | +% ----------------------------------------------------------------------------- |
| 249 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 250 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 251 | + fig, (ax1, ax2) = plt.subplots((1,2)) |
| 252 | + ax1.plot(Y1, X, color="C1") |
| 253 | + ax2.plot(Y2, X, color="C0") |
| 254 | +\end{lstlisting} |
| 255 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{plot-hsplit.pdf}} |
| 256 | +\end{tabular} |
| 257 | + |
| 258 | +% ----------------------------------------------------------------------------- |
| 259 | +\subsection*{\rmfamily Label \mdseries (everything)} |
| 260 | +% ----------------------------------------------------------------------------- |
| 261 | +% ----------------------------------------------------------------------------- |
| 262 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 263 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 264 | + plt.plot(X, Y) |
| 265 | + plt.suptitle(None) |
| 266 | + plt.title("A Sine wave") |
| 267 | +\end{lstlisting} |
| 268 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{plot-title.pdf}} |
| 269 | +\end{tabular} |
| 270 | +% ----------------------------------------------------------------------------- |
| 271 | +\begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 272 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 273 | + plt.plot(X, Y) |
| 274 | + plt.ylabel(None) |
| 275 | + plt.xlabel("Time") |
| 276 | +\end{lstlisting} |
| 277 | +& \raisebox{-0.75em}{\includegraphics[width=\linewidth]{plot-xlabel.pdf}} |
| 278 | +\end{tabular} |
| 279 | + |
| 280 | +% ----------------------------------------------------------------------------- |
| 281 | +\subsection*{\rmfamily Explore} |
| 282 | +% ----------------------------------------------------------------------------- |
| 283 | + |
| 284 | +Figures are shown with a graphical user interface that alllows to zoom |
| 285 | +and pan the figure, to navigate between the different views and to |
| 286 | +show the value under the mouse. |
| 287 | + |
| 288 | +% ----------------------------------------------------------------------------- |
| 289 | +\subsection*{\rmfamily Save \mdseries (bitmap or vector format)} |
| 290 | +% ----------------------------------------------------------------------------- |
| 291 | +\begin{lstlisting}[belowskip=-\baselineskip] |
| 292 | + plt.savefif("my-first-figure.png", dpi=300) |
| 293 | + plt.savefig("my-first-figure.pdf") |
| 294 | +\end{lstlisting} |
| 295 | +% |
| 296 | +\vfill |
| 297 | +% |
| 298 | +{\scriptsize Matplotlib handout for beginners. Copyright (c) |
| 299 | + 2020 Nicolas P. Rougier. Released under a CC-BY International 4.0 |
| 300 | + License. Supported by NumFocus Grant \#12345.\par} |
| 301 | + |
| 302 | +\end{multicols*} |
| 303 | +\end{document} |
| 304 | + |
0 commit comments