Visualizing vector fields

import matplotlib.pyplot as plt import numpy as np import inspect def f1(x): return np.tan(x) def f2(x): return np.sin(x) def f3(x): return np.cos(x) def f4(x): return np.exp(x/30)/(x**3) fs = [f1, f2, f3, f4] start, end = -10, 11 A = np.array([(i,j) for i in range(start,end) for j in range(start,end)]) for f in fs: B = f(A) plt.scatter(A[:,0], A[:,1], s=10, color='black') plt.scatter(B[:,0], B[:,1], s=10, color='black') for Ac, Bc in zip(A, B): Ax, Ay = Ac Bx, By = Bc plt.arrow(Ax, Ay, Bx, By, head_width=0.2, head_length=0.4, fc='k', ec='k', width=0.05) plt.title('f(x) = ' + inspect.getsource(f).split('return ')[1]) plt.tight_layout()

There is a tradeoff between looking at too many data points at once, but learning about some wider pattern and looking at too few data points, but seeing the arrows more clearly. Sometimes, instead of evaluating the function at all points, we can choose to do so only at specific coordinates.