|
| 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 | + set_minor_formatter, tick_params, fill_betweenx, text, |
| 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*{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*{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*{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*{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*{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*{Size \& DPI} |
| 157 | + |
| 158 | +Consider a square figure to be included in a two-columns A4 paper with |
| 159 | +2cm margins on each side and a column separation of 1cm. The width of |
| 160 | +a figure is (21 - 2*2 - 1)/2 = 8cm. One inch being 2.54cm, figure size |
| 161 | +should be 3.15$\times$3.15 inches. |
| 162 | +\begin{lstlisting}[] |
| 163 | + fig = plt.figure(figsize=(3.15,3.15), dpi=50) |
| 164 | + plt.savefig("figure.pdf", dpi=600) |
| 165 | +\end{lstlisting} |
| 166 | + |
| 167 | +%% \begin{tabular}{@{}m{.821\linewidth}m{.169\linewidth}} |
| 168 | +%% \begin{lstlisting}[belowskip=-\baselineskip] |
| 169 | +%% ax = plt.subplot(projection="polar") |
| 170 | +%% ax.set_rorigin(-3) |
| 171 | +%% plt.plot(X, Y, "C1-") |
| 172 | +%% \end{lstlisting} |
| 173 | +%% & \raisebox{-0.75em}{\includegraphics[width=\linewidth]{sine-polar.pdf}} |
| 174 | +%% \end{tabular} |
| 175 | + |
| 176 | + |
| 177 | +%% X = np.linspace(0, 2*np.pi, 10000) |
| 178 | +%% Y = np.sin(10*X) |
| 179 | + |
| 180 | +%% fig = plt.figure(figsize=(5,5)) |
| 181 | +%% ax = plt.subplot(projection="polar") |
| 182 | + |
| 183 | +%% ax.set_xticks(np.linspace(0, 2*np.pi, 8+1 )) |
| 184 | +%% ax.set_xticklabels([]) |
| 185 | +%% ax.set_yticklabels([]) |
| 186 | +%% plt.ylim(-1.5, 1.5) |
| 187 | +%% plt.plot(X, Y, "C1o-", markevery=500, mec="1.0", lw=2, ms=8.5, mew=2) |
| 188 | +%% plt.savefig("../figures/sine-polar.pdf", dpi=100) |
| 189 | +%% plt.show() |
| 190 | + |
| 191 | +\vfill |
| 192 | +% |
| 193 | +{\scriptsize Matplotlib 3.2 handout for intermediate users. Copyright |
| 194 | + (c) 2020 Nicolas P. Rougier. Released under a CC-BY 4.0 |
| 195 | + License. Supported by NumFocus Grant \#12345.\par} |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | +\end{multicols*} |
| 200 | +\end{document} |
| 201 | + |
0 commit comments