|
| 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