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

Skip to content

Commit 09c334c

Browse files
authored
Merge pull request matplotlib#7 from matplotlib/nicolas
Added performances tips
2 parents b4db1bc + 1d04c1b commit 09c334c

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ fonts*
33
*.log
44
*.out
55
*.aux
6+
figures/*.pdf

cheatsheets.tex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,23 @@
920920
\hspace*{2.5mm}~$\rightarrow$ plt.close("all")
921921
\end{myboxed}
922922

923+
\begin{myboxed}{Performance tips}
924+
{\ttfamily \fontsize{6pt}{7pt}\selectfont
925+
\textcolor{red}{ax.scatter(X, Y) \hfill slow}\\
926+
ax.plot(X, Y, marker="o", ls="") \hfill fast\\
927+
\hrule \smallskip
928+
\textcolor{red}{for i in range(0,n,2): \hfill (very) slow}\\
929+
\hspace*{2.5mm}~\textcolor{red}{plt.plot(X[i:i+2], Y[i:i+2])}\\
930+
931+
X0, Y0 = X[0::2], Y[0::2] \hfill fast\\
932+
X1, Y1 = X[1::2], Y[1::2]\\
933+
S = [None]*len(X)\\
934+
X = [v for t in zip(X0,X1,S) for v in t]\\
935+
Y = [v for t in zip(Y0,Y1,S) for v in t]\\
936+
plt.plot(X,Y)
937+
}
938+
\end{myboxed}
939+
923940
\end{multicols*}
924941
\end{document}
925942

scripts/performance-tips.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# -----------------------------------------------------------------------------
2+
# Matplotlib cheat sheet
3+
# Released under the BSD License
4+
# -----------------------------------------------------------------------------
5+
import time
6+
import numpy as np
7+
import matplotlib.pyplot as plt
8+
9+
10+
fig, ax = plt.subplots()
11+
12+
n = 10_000_000
13+
np.random.seed(1)
14+
X = np.random.uniform(0,1,n)
15+
Y = np.random.uniform(0,1,n)
16+
17+
start = time.perf_counter()
18+
ax.plot(X, Y, marker="o", ls="")
19+
end = time.perf_counter()
20+
print(f"Time: {end-start}s")
21+
22+
ax.clear()
23+
24+
start = time.perf_counter()
25+
ax.scatter(X, Y)
26+
end = time.perf_counter()
27+
print(f"Time: {end-start}s")
28+
29+
ax.clear()
30+
31+
n = 10_000
32+
np.random.seed(1)
33+
X = np.random.uniform(0,1,n)
34+
Y = np.random.uniform(0,1,n)
35+
36+
start = time.perf_counter()
37+
for i in range(0,n,2): plt.plot(X[i:i+2], Y[i:i+2])
38+
end = time.perf_counter()
39+
print(f"Time: {end-start}s")
40+
41+
ax.clear()
42+
43+
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)
50+
end = time.perf_counter()
51+
print(f"Time: {end-start}s")
52+

0 commit comments

Comments
 (0)