Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5fc487e

Browse files
committed
Simplified performance tips
1 parent f6103c3 commit 5fc487e

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

cheatsheets-1.png

2.98 KB
Loading

cheatsheets-2.png

52.3 KB
Loading

cheatsheets.tex

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@
782782
%
783783
\vspace{\fill}
784784
%
785-
\begin{myboxed}{Text aligmnents \hfill
785+
\begin{myboxed}{Text alignments \hfill
786786
\API{https://matplotlib.org/tutorials/text/text_props.html} }
787787
plt.\textbf{text}( …, ha=… , va=…, … )\\
788788

@@ -892,13 +892,16 @@
892892

893893

894894

895-
\begin{myboxed}{Color names}
895+
\begin{myboxed}{Color names \hfill
896+
\API{https://matplotlib.org/api/colors_api.html} }
896897
\includegraphics[width=\columnwidth]{colornames.pdf}
897898
\end{myboxed}
898899
%
899900
\vspace{\fill}
900901
%
901-
\begin{myboxed}{Image interpolation}
902+
\begin{myboxed}{Image interpolation
903+
\hfill \API{https://matplotlib.org/gallery/images_contours_and_fields/interpolation_methods.html} }
904+
\smallskip
902905
%% plt.\textbf{imshow}(…, interpolation=…)\\
903906
%% plt.\textbf{contour[f]}(…, interpolation=…)\\
904907
\includegraphics[width=\columnwidth]{interpolations.pdf}
@@ -947,6 +950,8 @@
947950
\hspace*{2.5mm}~$\rightarrow$ ax.set\_xticks([])\\
948951
\textbf{… remove tick labels ?}\\
949952
\hspace*{2.5mm}~$\rightarrow$ ax.set\_[xy]ticklabels([])\\
953+
\textbf{… rotate tick labels ?}\\
954+
\hspace*{2.5mm}~$\rightarrow$ plt.[xy]ticks(rotation=90)\\
950955
\textbf{… hide top spine?}\\
951956
\hspace*{2.5mm}~$\rightarrow$ ax.spines['top'].set\_visible(False)\\
952957
\textbf{… hide legend border?}\\
@@ -965,33 +970,30 @@
965970
\hspace*{2.5mm}~$\rightarrow$ plt.get\_cmap(``viridis\_r'')\\
966971
\textbf{… get a discrete colormap?}\\
967972
\hspace*{2.5mm}~$\rightarrow$ plt.get\_cmap(``viridis'', 10)\\
968-
\textbf{… rotate tick labels ?}\\
969-
\hspace*{2.5mm}~$\rightarrow$ plt.[xy]ticks(rotation=90)\\
970973
\textbf{… show a figure for one second?}\\
971974
\hspace*{2.5mm}~$\rightarrow$ plt.show(block=False), time.sleep(1)
972975
\end{myboxed}
973976
%
974977
\vspace{\fill}
975978
%
976979
\begin{myboxed}{Performance tips}
980+
\smallskip
977981
{\ttfamily \fontsize{6pt}{7pt}\selectfont
978-
\textcolor{red}{ax.scatter(X, Y) \hfill slow}\\
979-
ax.plot(X, Y, marker="o", ls="") \hfill fast\\
982+
\textcolor{red}{scatter(X, Y) \hfill slow}\\
983+
plot(X, Y, marker="o", ls="") \hfill fast\\
980984
\hrule \smallskip
981-
\textcolor{red}{for i in range(0,n,2): \hfill (very) slow}\\
982-
\hspace*{2.5mm}~\textcolor{red}{plt.plot(X[i:i+2], Y[i:i+2])}\\
983-
984-
X0, Y0 = X[0::2], Y[0::2] \hfill fast\\
985-
X1, Y1 = X[1::2], Y[1::2]\\
986-
S = [None]*len(X)\\
987-
X = [v for t in zip(X0,X1,S) for v in t]\\
988-
Y = [v for t in zip(Y0,Y1,S) for v in t]\\
989-
plt.plot(X,Y)}
985+
\textcolor{red}{for i in range(n): plot(X[i]) \hfill slow}\\
986+
plot(sum([x+[None] for x in X],[])) \hfill fast\\
987+
\hrule \smallskip
988+
\textcolor{red}{cla(), imshow(…), canvas.draw() \hfill slow}\\
989+
im.set\_data(…), canvas.draw() \hfill fast\smallskip
990+
}
990991
\end{myboxed}
991992
%
992993
\vspace{\fill}
993994
%
994995
\begin{myboxed}{Beyond Matplotlib}
996+
\smallskip
995997
\href{https://seaborn.pydata.org/}{\textbf{Seaborn}}: Statistical Data Visualization\\
996998
\href{https://scitools.org.uk/cartopy/docs/latest/}{\textbf{Cartopy}}: Geospatial Data Processing\\
997999
\href{https://yt-project.org/doc/index.html}{\textbf{yt}}: Volumetric data Visualization\\

scripts/performance-tips.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,30 @@
2828

2929
ax.clear()
3030

31-
n = 10_000
31+
n = 1_000
3232
np.random.seed(1)
33-
X = np.random.uniform(0,1,n)
34-
Y = np.random.uniform(0,1,n)
33+
X = []
34+
for i in range(n):
35+
X.append(np.random.uniform(0,1,10))
36+
#np.random.uniform(0,1,n)
37+
# Y = np.random.uniform(0,1,n)
3538

3639
start = time.perf_counter()
37-
for i in range(0,n,2): plt.plot(X[i:i+2], Y[i:i+2])
40+
# for i in range(0,n,2): plt.plot(X[i:i+2], Y[i:i+2])
41+
for i in range(n): plt.plot(X[i])
3842
end = time.perf_counter()
3943
print(f"Time: {end-start}s")
4044

4145
ax.clear()
4246

4347
start = time.perf_counter()
44-
X0,Y0 = X[0::2], Y[0::2]
45-
X1,Y1 = X[1::2], Y[1::2]
46-
S = [None]*len(X)
47-
X = [v for t in zip(X0,X1,S) for v in t]
48-
Y = [v for t in zip(Y0,Y1,S) for v in t]
49-
plt.plot(X,Y)
48+
ax.plot(sum([list(x)+[None] for x in X],[]))
49+
# X0,Y0 = X[0::2], Y[0::2]
50+
# X1,Y1 = X[1::2], Y[1::2]
51+
# S = [None]*len(X)
52+
# X = [v for t in zip(X0,X1,S) for v in t]
53+
# Y = [v for t in zip(Y0,Y1,S) for v in t]
54+
# plt.plot(X,Y)
5055
end = time.perf_counter()
5156
print(f"Time: {end-start}s")
5257

0 commit comments

Comments
 (0)