Typically you'd use imshow
or pcolormesh
for this.
For example:
import numpy as np
import matplotlib.pyplot as plt
n_points = 10
aa = np.linspace(-5, 5, n_points)
bb = np.linspace(-1.5, 1.5, n_points)
def cost(a, b):
return a + b
z = []
for a in aa:
for b in bb:
z.append(cost(a, b))
z = np.reshape(z, [len(aa), len(bb)])
fig, ax = plt.subplots()
im = ax.pcolormesh(aa, bb, z)
fig.colorbar(im)
ax.axis('tight')
plt.show()

However, it would be better to write your example code as:
import numpy as np
import matplotlib.pyplot as plt
n_points = 10
a = np.linspace(-5, 5, n_points)
b = np.linspace(-1.5, 1.5, n_points)
a, b = np.meshgrid(b, a)
z = a + b # Vectorize your cost function
fig, ax = plt.subplots()
im = ax.pcolormesh(a, b, z)
fig.colorbar(im)
ax.axis('tight')
plt.show()
Or, even more compactly:
import numpy as np
import matplotlib.pyplot as plt
npoints = 10
b, a = np.mgrid[-5:5:npoints*1j, -1.5:1.5:npoints*1j]
z = a + b
fig, ax = plt.subplots()
im = ax.pcolormesh(a, b, z)
fig.colorbar(im)
ax.axis('tight')
plt.show()