diff --git a/.gitignore b/.gitignore index 1965065..8d18ce4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -fonts* \ No newline at end of file +fonts* +*.upa +*.log +*.out +*.aux diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..419d7ed --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +Copyright (c) 2020, Nicolas P. Rougier + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index 8a5cd90..1c3ff20 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ -# cheatsheets -Various cheat sheets for matplotlib +# Cheatsheets + +![](./cheatsheets-1.png) + +![](./cheatsheets-2.png) # How to compile @@ -9,6 +12,7 @@ Various cheat sheets for matplotlib * `fonts/source-code-pro/*` : See https://fonts.google.com/specimen/Source+Code+Pro * `fonts/source-sans-pro/*` : See https://fonts.google.com/specimen/Source+Sans+Pro * `fonts/source-serif-pro/*` : See https://fonts.google.com/specimen/Source+Serif+Pro +* `fonts/delicious-123: See https://www.exljbris.com/delicious.html 2. You need to generate all the figures: @@ -23,6 +27,3 @@ $ cd .. $ xelatex cheatsheet-basic.tex $ xelatex cheatsheet-basic.tex ``` - - - diff --git a/cheatsheets-1.png b/cheatsheets-1.png new file mode 100644 index 0000000..55ebd74 Binary files /dev/null and b/cheatsheets-1.png differ diff --git a/cheatsheets-2.png b/cheatsheets-2.png new file mode 100644 index 0000000..922b6bc Binary files /dev/null and b/cheatsheets-2.png differ diff --git a/cheatsheet-basic.tex b/cheatsheets.tex similarity index 67% rename from cheatsheet-basic.tex rename to cheatsheets.tex index 5f2e38d..52f08b6 100644 --- a/cheatsheet-basic.tex +++ b/cheatsheets.tex @@ -1,6 +1,5 @@ % ----------------------------------------------------------------------------- -% Matplotlib cheat sheet -% Released under the BSD License +% Matplotlib cheat sheet - Released under the BSD License % ----------------------------------------------------------------------------- \documentclass[10pt,landscape,a4paper]{article} \usepackage[utf8]{inputenc} @@ -246,8 +245,10 @@ \includegraphics[width=\columnwidth]{logo.pdf} %\textbf{\Large \RobotoCon Matplotlib 3.2 cheat sheet}\\ %{\ttfamily https://matplotlib.org} \hfill CC-BY 4.0 - %\bigskip + % \bigskip \vspace{\fill} + %\hspace{1mm} \small \url{https://matplotlib.org/} + %\vspace{\fill} % --- Quick start ----------------------------------------------------------- \begin{myboxed}{Quick start \hfill @@ -274,7 +275,6 @@ \includegraphics[width=\columnwidth]{anatomy.pdf} \end{myboxed} \vspace{\fill} - % --- Layout --------------------------------------------------------------- \begin{myboxed}{Subplots layout \hfill \API{https://matplotlib.org/tutorials/intermediate/gridspec.html} } @@ -318,16 +318,21 @@ \begin{myboxed}{Basic plots} \plot{basic-plot.pdf}{\textbf{plot}([X],Y,[fmt],…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html} - {\paramx{optional}, - \paramy{mandatory}, - \paramfmt{optional}, - \paramcolor{optional}, - \parammarker{optional}, - \paramlinestyle{optional}} + {\optional{X}, + \mandatory{Y}, + \optional{fmt}, + \optional{color}, + \optional{marker}, + \optional{linestyle}} {} \plot{basic-scatter.pdf}{\textbf{scatter}(X,Y,…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html} - {x, y, [s]izes, [c]olors, markers, cmap} + {\mandatory{X}, + \mandatory{Y}, + \optional{[s]izes}, + \optional{[c]olors}, + \optional{markers}, + \optional{cmap}} {} \plot{basic-bar.pdf}{\textbf{bar[h]}(x,height,…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.bar.html} @@ -339,67 +344,134 @@ \optional{color} }{} \plot{basic-imshow.pdf}{\textbf{imshow}(Z,[cmap],…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html} - {\Z, \cmap, \interpolation{optional}, \extent, \origin} + { \mandatory{Z}, + \optional{cmap}, + \optional{interpolation}, + \optional{extent}, + \optional{origin} } {} - \plot{basic-contour.pdf}{\textbf{contour[f]}(Z,[n],…)} + \plot{basic-contour.pdf}{\textbf{contour[f]}([X],[Y],Z,,…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.contour.html} - {z, n, colors, extents, origin} + { \optional{X}, + \optional{Y}, + \mandatory{Z}, + \optional{levels}, + \optional{colors}, + \optional{extent}, + \optional{origin} } {} - \plot{basic-quiver.pdf}{\textbf{quiver}(X,Y,U,V,…)} + \plot{basic-quiver.pdf}{\textbf{quiver}([X],[Y],U,V,…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.quiver.html} - {x, y, fmt, color, marker, linestyle} + { \optional{X}, + \optional{Y}, + \mandatory{U}, + \mandatory{V}, + \optional{C}, + \optional{units}, + \optional{angles} } {} - \plot{basic-pie.pdf}{\textbf{pie}(Z,[n],…)} + \plot{basic-pie.pdf}{\textbf{pie}(X,[explode],…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html} - {z, n, colors, extents, origin} + {\mandatory{Z}, + \optional{explode}, + \optional{labels}, + \optional{colors}, + \optional{radius}} {} - \plot{basic-text.pdf}{\textbf{text}(X,Y,text,…)} - {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html} - {ha, va, size, weight, family, transform} + \plot{basic-text.pdf}{\textbf{text}(x,y,text,…)} + {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.text.html} + {\mandatory{x}, + \mandatory{y}, + \mandatory{text}, + \optional{va}, + \optional{ha}, + \optional{size}, + \optional{weight}, + \optional{transform} } {} - \plot{basic-fill.pdf}{\textbf{fill[\_between][x]}(…)} + \plot{basic-fill.pdf}{\textbf{fill[\_between][x]}( … )} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html} - {} + {\mandatory{X}, + \optional{Y1}, + \optional{Y2}, + \optional{color}, + \optional{where} } {} \end{myboxed} \vspace{\fill} % --- Advanced plots -------------------------------------------------------- \begin{myboxed}{Advanced plots} - \plot{advanced-step.pdf}{\textbf{step}([X],Y,[fmt],…)} - {https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.violinplot.html} - {x, y, fmt, color, marker, linestyle} + \plot{advanced-step.pdf}{\textbf{step}(X,Y,[fmt],…)} + {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.step.html} + {\mandatory{X}, + \mandatory{Y}, + \optional{fmt}, + \optional{color}, + \optional{marker}, + \optional{where} } {} - \plot{advanced-boxplot.pdf}{\textbf{boxplot}(D,…)} - {https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.violinplot.html} - {x, y, fmt, color, marker, linestyle} + \plot{advanced-boxplot.pdf}{\textbf{boxplot}(X,…)} + {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.boxplot.html} + { \mandatory{X}, + \optional{notch}, + \optional{sym}, + \optional{bootstrap}, + \optional{widths} } {} - \plot{advanced-errorbar.pdf}{\textbf{errorbar}(X,Y,E,…)} + \plot{advanced-errorbar.pdf}{\textbf{errorbar}(X,Y,xerr,yerr,…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.errorbar.html} - {x, y, fmt, color, marker, linestyle} + { \mandatory{X}, + \mandatory{Y}, + \optional{xerr}, + \optional{yerr}, + \optional{fmt} } {} - \plot{advanced-hist.pdf}{\textbf{hist}(X,bins,…)} + \plot{advanced-hist.pdf}{\textbf{hist}(X, bins, …)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html} - {x, y, fmt, color, marker, linestyle} + {\mandatory{X}, + \optional{bins}, + \optional{range}, + \optional{density}, + \optional{weights}} {} \plot{advanced-violin.pdf}{\textbf{violinplot}(D,…)} {https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.violinplot.html} - {x, y, fmt, color, marker, linestyle} + {\mandatory{D}, + \optional{positions}, + \optional{widths}, + \optional{vert} } {} - \plot{advanced-barbs.pdf}{\textbf{barbs}([X],[Y],U,V,…)} + \plot{advanced-barbs.pdf}{\textbf{barbs}([X],[Y], U, V, …)} {https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.violinplot.html} - {x, y, fmt, color, marker, linestyle} + { \optional{X}, + \optional{Y}, + \mandatory{U}, + \mandatory{V}, + \optional{C}, + \optional{length}, + \optional{pivot}, + \optional{sizes} } {} \plot{advanced-event.pdf}{\textbf{eventplot}(positions,…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.eventplot.html} - {x, y, fmt, color, marker, linestyle} + {\mandatory{positions}, + \optional{orientation}, + \optional{lineoffsets} } {} \plot{advanced-hexbin.pdf}{\textbf{hexbin}(X,Y,C,…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hexbin.html} - {x, y, fmt, color, marker, linestyle} + {\mandatory{X}, + \mandatory{Y}, + \optional{C}, + \optional{gridsize}, + \optional{bins} } {} \plot{advanced-xcorr.pdf}{\textbf{xcorr}(X,Y,…)} {https://matplotlib.org/api/_as_gen/matplotlib.pyplot.xcorr.html} - {x, y, fmt, color, marker, linestyle} + {\mandatory{X}, + \mandatory{Y}, + \optional{normed}, + \optional{detrend} } {} \end{myboxed} @@ -460,7 +532,7 @@ \API{https://matplotlib.org/tutorials/colors/colors.html}} % mpl.colors.to\_rbga(\textbf{color})\smallskip\\ \def\arraystretch{0.5} - \begin{tabular}{@{}p{0.7\columnwidth}p{0.3\columnwidth}@{}} + \begin{tabular}{@{}p{0.7\columnwidth}p{0.25\columnwidth}@{}} \palette{cycle}{'Cn'} \palette{raw}{ 'x' } \palette{name}{'name'} @@ -479,7 +551,7 @@ \API{https://matplotlib.org/tutorials/colors/colormaps.html}} {\ttfamily plt.\textbf{get\_cmap}(name) \smallskip\\} \def\arraystretch{0.5} - \begin{tabular}{@{}p{0.7\columnwidth}p{0.3\columnwidth}@{}} + \begin{tabular}{@{}p{0.7\columnwidth}p{0.25\columnwidth}@{}} \scriptsize \rule{0pt}{1.25em}Uniform & \\ \colormap{viridis} \colormap{magma} \colormap{plasma} % @@ -537,8 +609,14 @@ % {\ttfamily ax.\textbf{annotate}(…)} \hfill \api{https://matplotlib.org/api/_as_gen/matplotlib.pyplot.annotate.html}\\ - mappable, ax, cax, orientation \smallskip\\ - \includegraphics[width=\columnwidth]{colorbar.pdf}\\ + \mandatory{text}, + \mandatory{xy}, + \mandatory{xytext}, + \optional{xycoords}, + \optional{textcoords}, + \optional{arrowprops} + \smallskip\\ + \includegraphics[width=\columnwidth]{annotate.pdf}\\ % \end{myboxed} % @@ -581,22 +659,32 @@ \API{https://matplotlib.org/tutorials/introductory/customizing.html}} \setlength{\fboxsep}{0pt}% \setlength{\fboxrule}{.25pt}% - {\ttfamily plt.style.use(\textbf{style})\smallskip}\\ - \fbox{\includegraphics[width=.24\columnwidth]{style-default.pdf}} - \fbox{\includegraphics[width=.24\columnwidth]{style-classic.pdf}} - \fbox{\includegraphics[width=.24\columnwidth]{style-grayscale.pdf}} - \fbox{\includegraphics[width=.24\columnwidth]{style-ggplot.pdf}} - \fbox{\includegraphics[width=.24\columnwidth]{style-seaborn.pdf}} - \fbox{\includegraphics[width=.24\columnwidth]{style-fast.pdf}} - \fbox{\includegraphics[width=.24\columnwidth]{style-bmh.pdf}} - \fbox{\includegraphics[width=.24\columnwidth]{style-Solarize_Light2.pdf}} + {\ttfamily plt.style.use(\textbf{style})\medskip}\\ + \fbox{\includegraphics[width=.32\columnwidth]{style-default.pdf}} + \fbox{\includegraphics[width=.32\columnwidth]{style-classic.pdf}} + \fbox{\includegraphics[width=.32\columnwidth]{style-grayscale.pdf}} + \fbox{\includegraphics[width=.32\columnwidth]{style-ggplot.pdf}} + \fbox{\includegraphics[width=.32\columnwidth]{style-seaborn.pdf}} + \fbox{\includegraphics[width=.32\columnwidth]{style-fast.pdf}} + \fbox{\includegraphics[width=.32\columnwidth]{style-bmh.pdf}} + \fbox{\includegraphics[width=.32\columnwidth]{style-Solarize_Light2.pdf}} + \fbox{\includegraphics[width=.32\columnwidth]{style-seaborn-notebook.pdf}} + %% \fbox{\includegraphics[width=.24\columnwidth]{style-default.pdf}} + %% \fbox{\includegraphics[width=.24\columnwidth]{style-classic.pdf}} + %% \fbox{\includegraphics[width=.24\columnwidth]{style-grayscale.pdf}} + %% \fbox{\includegraphics[width=.24\columnwidth]{style-ggplot.pdf}} + %% \fbox{\includegraphics[width=.24\columnwidth]{style-seaborn.pdf}} + %% \fbox{\includegraphics[width=.24\columnwidth]{style-fast.pdf}} + %% \fbox{\includegraphics[width=.24\columnwidth]{style-bmh.pdf}} + %% \fbox{\includegraphics[width=.24\columnwidth]{style-Solarize_Light2.pdf}} \end{myboxed} % \vspace{\fill} % \begin{myboxed}{Quick reminder} {\ttfamily - ax.\textbf{grid}(), ax.patch.\textbf{set\_alpha}(0)\\ + ax.\textbf{grid}()\\ + ax.patch.\textbf{set\_alpha}(0)\\ ax.\textbf{set\_[xy]lim}(vmin, vmax)\\ ax.\textbf{set\_[xy]label}(label)\\ ax.\textbf{set\_[xy]ticks}(list)\\ @@ -658,12 +746,180 @@ 8. Avoid “Chartjunk”\\ 9. Message Trumps Beauty\\ 10. Get the Right Tool + \end{myboxed} +\end{multicols*} + +\begin{multicols*}{5} + \begin{myboxed}{Axes adjustements\hfill + \API{https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots_adjust.html}} + plt.\textbf{subplot\_adjust}( … )\\ + + \includegraphics[width=\columnwidth]{adjustments.pdf} \end{myboxed} - + % + \vspace{\fill} + % + \begin{myboxed}{Extent \& origin \hfill + \API{https://matplotlib.org/tutorials/intermediate/imshow_extent.html} } + plt.\textbf{imshow}( extent=…, origin=… )\\ + \includegraphics[width=\columnwidth]{extents.pdf} + \end{myboxed} + % + \vspace{\fill} + % + \begin{myboxed}{Text aligmnents \hfill + \API{https://matplotlib.org/tutorials/text/text_props.html} } + plt.\textbf{text}( …, ha=… , va=…, … )\\ - - + \includegraphics[width=\columnwidth]{text-alignments.pdf} + \end{myboxed} + % + \vspace{\fill} + % + \begin{myboxed}{Text parameters \hfill + \API{https://matplotlib.org/tutorials/text/text_props.html}} + plt.\textbf{text}( …, family=… , size=…, weight = …)\\ + plt.\textbf{text}( …, fontproperties = … )\\ + + \includegraphics[width=\columnwidth]{fonts.pdf} + \end{myboxed} + + + \begin{myboxed}{Uniform colormaps} + \begin{tabular}{@{}p{0.7\columnwidth}p{0.25\columnwidth}@{}} + \scriptsize \rule{0pt}{1.25em}\noindent + \colormap{viridis} + \colormap{plasma} + \colormap{inferno} + \colormap{magma} + \colormap{cividis} + \end{tabular} + \end{myboxed} + % + \vspace{\fill} + % + \begin{myboxed}{Sequential colormaps} + \begin{tabular}{@{}p{0.7\columnwidth}p{0.25\columnwidth}@{}} + \scriptsize \rule{0pt}{1.25em}\noindent + \colormap{Greys} + \colormap{Purples} + \colormap{Blues} + \colormap{Greens} + \colormap{Oranges} + \colormap{Reds} + \colormap{YlOrBr} + \colormap{YlOrRd} + \colormap{OrRd} + \colormap{PuRd} + \colormap{RdPu} + \colormap{BuPu} + \colormap{GnBu} + \colormap{PuBu} + \colormap{YlGnBu} + \colormap{PuBuGn} + \colormap{BuGn} + \colormap{YlGn} + \end{tabular} + \end{myboxed} + % + \vspace{\fill} + % + \begin{myboxed}{Diverging colormaps} + \begin{tabular}{@{}p{0.7\columnwidth}p{0.25\columnwidth}@{}} + \scriptsize \rule{0pt}{1.25em}\noindent + \colormap{PiYG} + \colormap{PRGn} + \colormap{BrBG} + \colormap{PuOr} + \colormap{RdGy} + \colormap{RdBu} + \colormap{RdYlBu} + \colormap{RdYlGn} + \colormap{Spectral} + \colormap{coolwarm} + \colormap{bwr} + \colormap{seismic} + \end{tabular} + \end{myboxed} + % + \vspace{\fill} + % + \begin{myboxed}{Qualitative colormaps} + \begin{tabular}{@{}p{0.7\columnwidth}p{0.25\columnwidth}@{}} + \scriptsize \rule{0pt}{1.25em}\noindent + \colormap{Pastel1} + \colormap{Pastel2} + \colormap{Paired} + \colormap{Accent} + \colormap{Dark2} + \colormap{Set1} + \colormap{Set2} + \colormap{Set3} + \colormap{tab10} + \colormap{tab20} + \colormap{tab20b} + \colormap{tab20c} + \end{tabular} + \end{myboxed} + % + \vspace{\fill} + % + \begin{myboxed}{Miscellaneous colormaps} + \begin{tabular}{@{}p{0.7\columnwidth}p{0.25\columnwidth}@{}} + \scriptsize \rule{0pt}{1.25em}\noindent + \colormap{terrain} + \colormap{ocean} + \colormap{cubehelix} + \colormap{rainbow} + \colormap{twilight} + \end{tabular} + \end{myboxed} + + \begin{myboxed}{Color names} + \includegraphics[width=\columnwidth]{colornames.pdf} + \end{myboxed} + % + \vspace{\fill} + % + \begin{myboxed}{Legend placement} + \includegraphics[width=\columnwidth]{legend-placement.pdf} + plt.\textbf{legend}(loc="string", bbox\_to\_anchor=(x,y))\\ + \begin{tabular}{@{}p{0.33\columnwidth} + p{0.33\columnwidth} + p{0.33\columnwidth}@{}} + \scriptsize \rule{0pt}{1.25em}\noindent + 1: lower left & 2: lower center & 3: lower right\\ + 4: left & 5: center & 6: right\\ + 7: upper left & 8: upper center & 9: upper right\\ + \end{tabular} + + \begin{tabular}{@{}p{0.495\columnwidth} + p{0.495\columnwidth}@{}} + \scriptsize \rule{0pt}{1.25em}\noindent + A: upper right / (-.1,.9) & B: right / (-.1,.5)\\ + C: lower right / (-.1,.1) & D: upper left / (-.1,-.1)\\ + E: upper center / (.5,-.1) & F: upper right / (.9,-.1)\\ + G: lower left / (1.1,.1) & H: left / (1.1,.5)\\ + I: upper left / (1.1,.9) & J: lower right / (.9,1.1)\\ + K: lower center / (.5,1.1) & L: lower left / (.1,1.1) + \end{tabular} + + \end{myboxed} + % + \vspace{\fill} + % + \begin{myboxed}{How do I …} + \textbf{… resize a figure?}\\ + \hspace*{2.5mm}~$\rightarrow$ fig.set\_size\_inches(w,h)\\ + \textbf{… save a figure?}\\ + \hspace*{2.5mm}~$\rightarrow$ plt.savefig("figure.pdf")\\ + \textbf{… clear a figure?}\\ + \hspace*{2.5mm}~$\rightarrow$ ax.clear()\\ + \textbf{… close all figures?}\\ + \hspace*{2.5mm}~$\rightarrow$ plt.close("all") + \end{myboxed} + \end{multicols*} \end{document} diff --git a/scripts/adjustements.py b/scripts/adjustements.py new file mode 100644 index 0000000..1f2aa88 --- /dev/null +++ b/scripts/adjustements.py @@ -0,0 +1,129 @@ +# ----------------------------------------------------------------------------- +# Matplotlib cheat sheet +# Released under the BSD License +# ----------------------------------------------------------------------------- + +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.patches as mpatches +from matplotlib.collections import PatchCollection + + +fig = plt.figure(figsize=(4.25, 4.25 * 95/115)) +ax = fig.add_axes([0,0,1,1], frameon=False, aspect=1, + xlim=(0-5,100+10), ylim=(-10,80+5), xticks=[], yticks=[]) + + +box = mpatches.FancyBboxPatch( + (0,0), 100, 83, mpatches.BoxStyle("Round", pad=0, rounding_size=2), + linewidth=1., facecolor="0.9", edgecolor="black") +ax.add_artist(box) + +box = mpatches.FancyBboxPatch( + (0,0), 100, 75, mpatches.BoxStyle("Round", pad=0, rounding_size=0), + linewidth=1., facecolor="white", edgecolor="black") +ax.add_artist(box) + + +box = mpatches.Rectangle( + (5,5), 45, 30, zorder=10, + linewidth=1.0, facecolor="white", edgecolor="black") +ax.add_artist(box) + +box = mpatches.Rectangle( + (5,40), 45, 30, zorder=10, + linewidth=1.0, facecolor="white", edgecolor="black") +ax.add_artist(box) + +box = mpatches.Rectangle( + (55,5), 40, 65, zorder=10, + linewidth=1.0, facecolor="white", edgecolor="black") +ax.add_artist(box) + +# Window button +X, Y = [5,10,15], [79,79,79] +plt.scatter(X, Y, s=75, zorder=10, + edgecolor="black", facecolor="white", linewidth=1) + + +# Window size extension +X, Y = [0, 0], [0, -8] +plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) + +X, Y = [100, 100], [0, -8] +plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) + +X, Y = [100, 108], [0, 0] +plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) + +X, Y = [100, 108], [75, 75] +plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) + + +def ext_arrow(p0,p1,p2,p3): + p0, p1 = np.asarray(p0), np.asarray(p1) + p2, p3 = np.asarray(p2), np.asarray(p3) + ax.arrow(*p0, *(p1-p0), zorder=20, linewidth=0, + length_includes_head=True, width=.4, + head_width=2, head_length=2, color="black") + ax.arrow(*p3, *(p2-p3), zorder=20, linewidth=0, + length_includes_head=True, width=.4, + head_width=2, head_length=2, color="black") + plt.plot([p1[0],p2[0]], [p1[1],p2[1]], linewidth=.9, color="black") + +def int_arrow(p0,p1): + p0, p1 = np.asarray(p0), np.asarray(p1) + ax.arrow(*((p0+p1)/2), *((p1-p0)/2), zorder=20, linewidth=0, + length_includes_head=True, width=.4, + head_width=2, head_length=2, color="black") + ax.arrow(*((p0+p1)/2), *(-(p1-p0)/2), zorder=20, linewidth=0, + length_includes_head=True, width=.4, + head_width=2, head_length=2, color="black") + + + +x = 0 +y = 10 +ext_arrow( (x-4,y), (x,y), (x+5,y), (x+9,y) ) +ax.text(x+9.5, y, "left", ha="left", va="center", size="x-small", zorder=20) + +x += 50 +ext_arrow( (x-4,y), (x,y), (x+5,y), (x+9,y) ) +ax.text(x-4.5, y, "wspace", ha="right", va="center", size="x-small", zorder=20) + +x += 45 +ext_arrow( (x-4,y), (x,y), (x+5,y), (x+9,y) ) +ax.text(x-4.5, y, "right", ha="right", va="center", size="x-small", zorder=20) + +y = 0 +x = 25 +ext_arrow( (x,y-4), (x,y), (x,y+5), (x,y+9) ) +ax.text(x, y+9.5, "bottom", ha="center", va="bottom", size="x-small", zorder=20) + +y += 35 +ext_arrow( (x,y-4), (x,y), (x,y+5), (x,y+9) ) +ax.text(x, y-4.5, "hspace", ha="center", va="top", size="x-small", zorder=20) + +y += 35 +ext_arrow( (x,y-4), (x,y), (x,y+5), (x,y+9) ) +ax.text(x, y-4.5, "top", ha="center", va="top", size="x-small", zorder=20) + +int_arrow((0,-5), (100,-5)) +ax.text(50, -5, "figure width", backgroundcolor="white", zorder=30, + ha="center", va="center", size="x-small") + +int_arrow((105,0), (105,75)) +ax.text(105, 75/2, "figure height", backgroundcolor="white", zorder=30, + rotation = "vertical", ha="center", va="center", size="x-small") + +int_arrow((55,62.5), (95,62.5)) +ax.text(75, 62.5, "axes width", backgroundcolor="white", zorder=30, + ha="center", va="center", size="x-small") + +int_arrow((62.5,5), (62.5,70)) +ax.text(62.5, 35, "axes height", backgroundcolor="white", zorder=30, + rotation = "vertical", ha="center", va="center", size="x-small") + + +plt.savefig("../figures/adjustments.pdf") +# plt.show() diff --git a/scripts/annotate.py b/scripts/annotate.py new file mode 100644 index 0000000..a544bc9 --- /dev/null +++ b/scripts/annotate.py @@ -0,0 +1,28 @@ +# ----------------------------------------------------------------------------- +# Matplotlib cheat sheet +# Released under the BSD License +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib as mpl +import matplotlib.pyplot as plt + + +fig = plt.figure(figsize=(6,1)) +#ax = plt.subplot(111, frameon=False, aspect=.1) +# b = 0.0 +ax = fig.add_axes([0,0,1,1], frameon=False, aspect=1) + + + +plt.scatter([5.5],[0.75], s=100, c="k") +plt.xlim(0,6), plt.ylim(0,1) +plt.xticks([]), plt.yticks([]) + +plt.annotate("Annotation", (5.5,.75), (0.1,.75), size=16, va="center", + arrowprops=dict(facecolor='black', shrink=0.05)) + +plt.text( 5.5, 0.6, "xy\nycoords", size=10, va="top", ha="center", color=".5") +plt.text( .75, 0.6, "xytext\ntextcoords", size=10, va="top", ha="center", color=".5") + +plt.savefig("../figures/annotate.pdf") +# plt.show() diff --git a/scripts/colornames.py b/scripts/colornames.py new file mode 100644 index 0000000..7d66716 --- /dev/null +++ b/scripts/colornames.py @@ -0,0 +1,57 @@ +""" +======================== +Visualizing named colors +======================== + +Simple plot example with the named colors and its visual representation. +""" +from __future__ import division + +import matplotlib.pyplot as plt +from matplotlib import colors as mcolors + + +colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS) + +# Sort colors by hue, saturation, value and name. +by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name) + for name, color in colors.items()) +sorted_names = [name for hsv, name in by_hsv] + +n = len(sorted_names) +ncols = 3 +nrows = n // ncols + 1 + +fig, ax = plt.subplots(figsize=(4.5, 6)) + +# Get height and width +X, Y = fig.get_dpi() * fig.get_size_inches() +h = Y / (nrows + 1) +w = X / ncols + +for i, name in enumerate(sorted_names): + col = i // nrows + row = i % nrows + y = Y - (row * h) - h + + xi_line = w * (col + 0.05) + xf_line = w * (col + 0.25) + xi_text = w * (col + 0.3) + + ax.text(xi_text, y, name, fontsize=7, + horizontalalignment='left', + verticalalignment='center') + + ax.hlines(y + h * 0.1, xi_line, xf_line, + color=colors[name], linewidth=(h * 0.6)) + +ax.set_xlim(0, X) +ax.set_ylim(0, Y) +ax.set_axis_off() + +fig.subplots_adjust(left=0, right=1, + top=1, bottom=0, + hspace=0, wspace=0) + +plt.savefig("../figures/colornames.pdf") +# plt.show() diff --git a/scripts/extents.py b/scripts/extents.py new file mode 100644 index 0000000..1fa736c --- /dev/null +++ b/scripts/extents.py @@ -0,0 +1,69 @@ +# ----------------------------------------------------------------------------- +# Matplotlib cheat sheet +# Released under the BSD License +# ----------------------------------------------------------------------------- + +# Scripts to generate all the basic plots +import numpy as np +import matplotlib as mpl +import matplotlib.pyplot as plt + +Z = np.arange(5*5).reshape(5,5) + +fig = plt.figure(figsize=(8,5)) + +ax = fig.add_subplot(2,2,1) +ax.imshow(Z, extent=[0,10,0,5], interpolation="nearest", origin="upper") +ax.set_xlim(-1, 11), ax.set_xticks([]) +ax.set_ylim(-1, 6), ax.set_yticks([0,5]) +ax.text(1, 4.5, "(0,0)", ha="center", va="center", color="white", size="large") +ax.text(9, 0.5, "(4,4)", ha="center", va="center", color="black", size="large") +ax.text(5.0, 5.5, 'origin="upper"', + ha="center", va="center", color="black", size="large") +ax.text(5.0, -0.5, "extent=[0,10,0,5]", + ha="center", va="center", color="black", size="large") + +ax = fig.add_subplot(2,2,3) +ax.imshow(Z, extent=[0,10,0,5], interpolation="nearest", origin="lower") +ax.set_xlim(-1, 11), ax.set_xticks([0,10]) +ax.set_ylim(-1, 6), ax.set_yticks([0,5]) +ax.text(1, 0.5, "(0,0)", ha="center", va="center", color="white", size="large") +ax.text(9, 4.5, "(4,4)", ha="center", va="center", color="black", size="large") + +ax.text(5.0, 5.5, 'origin="lower"', + ha="center", va="center", color="black", size="large") +ax.text(5.0, -0.5, "extent=[0,10,0,5]", + ha="center", va="center", color="black", size="large") + + +ax = fig.add_subplot(2,2,4) +ax.imshow(Z, extent=[10,0,0,5], interpolation="nearest", origin="lower") +ax.set_xlim(-1, 11), ax.set_xticks([0,10]) +ax.set_ylim(-1, 6), ax.set_yticks([]) +ax.text(9, 0.5, "(0,0)", ha="center", va="center", color="white", size="large") +ax.text(1, 4.5, "(4,4)", ha="center", va="center", color="black", size="large") +ax.text(5.0, 5.5, 'origin="lower"', + ha="center", va="center", color="black", size="large") +ax.text(5.0, -0.5, "extent=[10,0,0,5]", + ha="center", va="center", color="black", size="large") +plt.tight_layout() + + +ax = fig.add_subplot(2,2,2) +ax.imshow(Z, extent=[10,0,0,5], interpolation="nearest", origin="upper") +ax.set_xlim(-1, 11), ax.set_xticks([]) +ax.set_ylim(-1, 6), ax.set_yticks([]) +ax.text(9, 4.5, "(0,0)", ha="center", va="center", color="white", size="large") +ax.text(1, 0.5, "(4,4)", ha="center", va="center", color="black", size="large") +ax.text(5.0, 5.5, 'origin="lower"', + ha="center", va="center", color="black", size="large") +ax.text(5.0, -0.5, "extent=[10,0,0,5]", + ha="center", va="center", color="black", size="large") +plt.tight_layout() + + +plt.savefig("../figures/extents.pdf", dpi=600) +plt.show() + + + diff --git a/scripts/legend.py b/scripts/legend.py new file mode 100644 index 0000000..efc55ea --- /dev/null +++ b/scripts/legend.py @@ -0,0 +1,50 @@ +# ----------------------------------------------------------------------------- +# Matplotlib cheat sheet +# Released under the BSD License +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib as mpl +import matplotlib.pyplot as plt + + +fig = plt.figure(figsize=(4,4)) +ax = fig.add_axes([0.15,0.15,.7,.7], frameon=True, aspect=1, + xticks=[], yticks=[]) + +def text(x, y, _text): + color= "C1" + if not 0 < x < 1 or not 0 < y < 1: color = "C0" + size = 0.15 + ax.text(x, y, _text, color="white", #bbox={"color": "C1"}, + size="xx-large", weight="bold", ha="center", va="center") + rect = plt.Rectangle((x-size/2, y-size/2), size, size, facecolor=color, + zorder=-10, clip_on=False) + ax.add_patch(rect) + +def point(x, y): + ax.scatter([x], [y], facecolor="C0", edgecolor="white", + zorder=10, clip_on=False) + +d = .1 +e = .15/2 + +text( d, d, "1"), text( 0.5, d, "2"), text(1-d, d, "3") +text( d, 0.5, "4"), text( 0.5, 0.5, "5"), text(1-d, 0.5, "6") +text( d, 1-d, "7"), text( 0.5, 1-d, "8"), text(1-d, 1-d, "9") + +text( -d, 1-d, "A"), text( -d, 0.5, "B"), text( -d, d, "C") +point(-d+e, 1-d+e), point(-d+e, 0.5), point(-d+e, d-e), + +text( d, -d, "D"), text(0.5, -d, "E"), text( 1-d, -d, "F") +point(d-e, -d+e), point(0.5, -d+e), point(1-d+e, -d+e), + +text(1+d, d, "G"), text(1+d, 0.5, "H"), text( 1+d, 1-d, "I") +point(1+d-e, d-e), point(1+d-e, .5), point(1+d-e, 1-d+e), + +text(1-d, 1+d, "J"), text(0.5, 1+d, "K"), text( d, 1+d, "L") +point(1-d+e, 1+d-e), point(0.5, 1+d-e), point(d-e, 1+d-e), + +plt.xlim(0,1), plt.ylim(0,1) + +plt.savefig("../figures/legend-placement.pdf") +# plt.show() diff --git a/scripts/projections.py b/scripts/projections.py index 094bf9c..8178f7c 100644 --- a/scripts/projections.py +++ b/scripts/projections.py @@ -54,4 +54,3 @@ ax.add_feature(cartopy.feature.LAND, zorder=0, facecolor="C1", edgecolor="0.0", linewidth=0) plt.savefig("../figures/projection-cartopy.pdf") - diff --git a/scripts/text-alignments.py b/scripts/text-alignments.py new file mode 100644 index 0000000..2470ab7 --- /dev/null +++ b/scripts/text-alignments.py @@ -0,0 +1,70 @@ +# ----------------------------------------------------------------------------- +# Matplotlib cheat sheet +# Released under the BSD License +# ----------------------------------------------------------------------------- +import numpy as np +import matplotlib.pyplot as plt + +dpi = 100 +fig = plt.figure(figsize=(4.25, 1.5), dpi=dpi) +ax = fig.add_axes([0,0,1,1], frameon=False, + xlim=(0,4.25), ylim=(0,1.5), xticks=[], yticks=[]) + +fontsize = 48 +renderer = fig.canvas.get_renderer() +horizontalalignment = "left" +verticalalignment = "center" +position = (0.25, 1.5/2) +color = "0.25" + +# Compute vertical and horizontal alignment offsets +text = ax.text(0, 0, "Matplotlib", fontsize=fontsize) +yoffset = {} +for alignment in ["top", "center", "baseline", "bottom"]: + text.set_verticalalignment(alignment) + y = text.get_window_extent(renderer).y0/dpi + yoffset[alignment] = y + +xoffset = {} +for alignment in ["left", "center", "right"]: + text.set_horizontalalignment(alignment) + x = text.get_window_extent(renderer).x0/dpi + xoffset[alignment] = x + +# Actual positioning of the text +text.set_horizontalalignment(horizontalalignment) +text.set_verticalalignment(verticalalignment) +text.set_position(position) + + +for name,y in yoffset.items(): + y = position[1] - y + yoffset[verticalalignment] + plt.plot([0.1, 3.75], [y, y], linewidth=0.5, color=color) + plt.text(3.75, y, " "+name, color=color, + ha="left", va="center", size="x-small") + +for name,x in xoffset.items(): + x = position[0] - x + xoffset[horizontalalignment] + plt.plot([x,x], [0.25, 1.25], linewidth=0.5, color=color) + plt.text(x, 0.24, name, color = color, + ha="center", va="top", size="x-small") + +P = [] +for x in xoffset.values(): + x = position[0] - x + xoffset[horizontalalignment] + for y in yoffset.values(): + y = position[1] - y + yoffset[verticalalignment] + P.append((x,y)) +P = np.array(P) + +ax.scatter(P[:,0], P[:,1], s=10, zorder=10, + facecolor="white", edgecolor=color, linewidth=0.75) + +epsilon = 0.05 +plt.text(P[3,0]+epsilon, P[3,1]-epsilon, "(0,0)", + color=color, ha="left", va="top", size="x-small") +plt.text(P[8,0]-epsilon, P[8,1]+epsilon, "(1,1)", + color=color, ha="right", va="bottom", size="x-small") + +plt.savefig("../figures/text-alignments.pdf") +# plt.show()