Skip to content

Commit 6e1ac2c

Browse files
authored
Merge pull request #8 from matplotlib/nicolas
Added tips and interpolation
2 parents 09c334c + 56612ff commit 6e1ac2c

File tree

5 files changed

+92
-15
lines changed

5 files changed

+92
-15
lines changed

cheatsheets-1.png

1.71 KB
Loading

cheatsheets-2.png

255 KB
Loading

cheatsheets.tex

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,23 @@
217217
\newenvironment{myboxed}[1]
218218
{\begin{mdframed}[linecolor=black,
219219
backgroundcolor=white,
220-
outerlinewidth=.1pt,
221-
roundcorner=0.25em,
220+
outerlinewidth=0.25pt,
221+
%roundcorner=0.25em,
222222
innertopmargin=1ex,
223+
topline=true,
224+
rightline=true,
225+
leftline=true,
226+
bottomline=true,
227+
linecolor=black!10,
223228
frametitleaboveskip=0.5em,
224229
frametitlebelowskip=0.5em,
225230
innerbottommargin=.5\baselineskip,
226-
innerrightmargin=.5em, innerleftmargin=.5em,
231+
innerrightmargin=.5em,
232+
innerleftmargin=.5em,
227233
%userdefinedwidth=1\textwidth,
228-
frametitle={\sffamily \textbf{#1}},
234+
frametitle={\scshape \bfseries \sffamily #1},
235+
% frametitlerule=true,
236+
%frametitlerulecolor=red,
229237
frametitlebackgroundcolor=black!10,
230238
frametitlerulewidth=2pt]}
231239
{\end{mdframed}}
@@ -876,12 +884,21 @@
876884
\end{tabular}
877885
\end{myboxed}
878886

887+
888+
879889
\begin{myboxed}{Color names}
880890
\includegraphics[width=\columnwidth]{colornames.pdf}
881891
\end{myboxed}
882892
%
883893
\vspace{\fill}
884894
%
895+
\begin{myboxed}{Image interpolation}
896+
plt.\textbf{imshow}(…, interpolation=…)\\
897+
plt.\textbf{contour[f]}(…, interpolation=…)\\
898+
\includegraphics[width=\columnwidth]{interpolations.pdf}
899+
\end{myboxed}
900+
901+
885902
\begin{myboxed}{Legend placement}
886903
\includegraphics[width=\columnwidth]{legend-placement.pdf}
887904
plt.\textbf{legend}(loc="string", bbox\_to\_anchor=(x,y))\\
@@ -903,23 +920,51 @@
903920
G: lower left / (1.1,.1) & H: left / (1.1,.5)\\
904921
I: upper left / (1.1,.9) & J: lower right / (.9,1.1)\\
905922
K: lower center / (.5,1.1) & L: lower left / (.1,1.1)
906-
\end{tabular}
907-
923+
\end{tabular}
908924
\end{myboxed}
909-
%
910-
\vspace{\fill}
925+
926+
\vfill\null \columnbreak
927+
911928
%
912929
\begin{myboxed}{How do I …}
913930
\textbf{… resize a figure?}\\
914931
\hspace*{2.5mm}~$\rightarrow$ fig.set\_size\_inches(w,h)\\
915932
\textbf{… save a figure?}\\
916933
\hspace*{2.5mm}~$\rightarrow$ plt.savefig("figure.pdf")\\
934+
\textbf{… save a transparent figure?}\\
935+
\hspace*{2.5mm}~$\rightarrow$ plt.savefig("figure.pdf", transparent=True)\\
917936
\textbf{… clear a figure?}\\
918937
\hspace*{2.5mm}~$\rightarrow$ ax.clear()\\
919938
\textbf{… close all figures?}\\
920-
\hspace*{2.5mm}~$\rightarrow$ plt.close("all")
939+
\hspace*{2.5mm}~$\rightarrow$ plt.close("all")\\
940+
\textbf{… remove ticks?}\\
941+
\hspace*{2.5mm}~$\rightarrow$ ax.set\_xticks([])\\
942+
\textbf{… remove tick labels ?}\\
943+
\hspace*{2.5mm}~$\rightarrow$ ax.set\_[xy]ticklabels([])\\
944+
\textbf{… hide top spine?}\\
945+
\hspace*{2.5mm}~$\rightarrow$ ax.spines['top'].set\_visible(False)\\
946+
\textbf{… hide legend border?}\\
947+
\hspace*{2.5mm}~$\rightarrow$ plt.legend(frameon=False)\\
948+
\textbf{… show error as shaded region?}\\
949+
\hspace*{2.5mm}~$\rightarrow$ ax.fill\_between(X, Y+error, Y-error)\\
950+
\textbf{… draw a rectangle?}\\
951+
\hspace*{2.5mm}~$\rightarrow$ ax.add\_patch(plt.Rectangle((0, 0),1,1)\\
952+
\textbf{… draw outside frame?}\\
953+
\hspace*{2.5mm}~$\rightarrow$ ax.plot(…, clip\_on=False)\\
954+
\textbf{… use transparency?}\\
955+
\hspace*{2.5mm}~$\rightarrow$ ax.plot(…, alpha=0.25)\\
956+
\textbf{… set figure background color?}\\
957+
\hspace*{2.5mm}~$\rightarrow$ fig.patch.set\_facecolor(``grey'')\\
958+
\textbf{… get a reversed colormap?}\\
959+
\hspace*{2.5mm}~$\rightarrow$ plt.get\_cmap(``viridis\_r'')\\
960+
\textbf{… get a discrete colormap?}\\
961+
\hspace*{2.5mm}~$\rightarrow$ plt.get\_cmap(``viridis'', 10)
962+
%% \textbf{… show a figure for one second?}\\
963+
%% \hspace*{2.5mm}~$\rightarrow$ plt.show(block=False), time.sleep(1)
921964
\end{myboxed}
922-
965+
%
966+
\vspace{\fill}
967+
%
923968
\begin{myboxed}{Performance tips}
924969
{\ttfamily \fontsize{6pt}{7pt}\selectfont
925970
\textcolor{red}{ax.scatter(X, Y) \hfill slow}\\
@@ -933,8 +978,18 @@
933978
S = [None]*len(X)\\
934979
X = [v for t in zip(X0,X1,S) for v in t]\\
935980
Y = [v for t in zip(Y0,Y1,S) for v in t]\\
936-
plt.plot(X,Y)
937-
}
981+
plt.plot(X,Y)}
982+
\end{myboxed}
983+
%
984+
\vspace{\fill}
985+
%
986+
\begin{myboxed}{Beyond Matplotlib}
987+
\href{https://seaborn.pydata.org/}{\textbf{Seaborn}}: Statistical Data Visualization\\
988+
\href{https://scitools.org.uk/cartopy/docs/latest/}{\textbf{Cartopy}}: Geospatial Data Processing\\
989+
\href{https://yt-project.org/doc/index.html}{\textbf{yt}}: Volumetric data Visualization\\
990+
\href{https://mpld3.github.io}{\textbf{mpld3}}: Bringing Matplotlib to the browser\\
991+
\href{https://datashader.org/}{\textbf{Datashader}}: Large data processing pipeline\\
992+
\href{https://plotnine.readthedocs.io/en/latest/}{\textbf{plotnine}}: A Grammar of Graphics for Python
938993
\end{myboxed}
939994

940995
\end{multicols*}

scripts/colornames.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
n = len(sorted_names)
2222
ncols = 3
23-
nrows = n // ncols + 1
23+
nrows = n // ncols +1
2424

2525
fig, ax = plt.subplots(figsize=(4.5, 6))
2626

@@ -30,8 +30,8 @@
3030
w = X / ncols
3131

3232
for i, name in enumerate(sorted_names):
33-
col = i // nrows
34-
row = i % nrows
33+
col = i // (nrows-1)
34+
row = i % (nrows-1)
3535
y = Y - (row * h) - h
3636

3737
xi_line = w * (col + 0.05)

scripts/interpolations.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16',
5+
'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',
6+
'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos']
7+
8+
np.random.seed(1)
9+
Z = np.random.uniform(0,1,(3,3))
10+
11+
12+
fig, axs = plt.subplots(nrows=6, ncols=3, figsize=(4.5,9),
13+
subplot_kw={'xticks': [], 'yticks': []})
14+
for ax, interp_method in zip(axs.flat, methods):
15+
ax.imshow(Z, interpolation=interp_method, cmap='viridis',
16+
extent=[0,9,0,9])
17+
ax.text(4.5, 1, str(interp_method), weight="bold", color="white", size=12,
18+
transform=ax.transData, ha="center", va="center")
19+
20+
plt.tight_layout()
21+
plt.savefig("../figures/interpolations.pdf", dpi=600)
22+
plt.show()

0 commit comments

Comments
 (0)