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