'How can I plot a square and colour map it using a 2d array of values

For example I have 2 arrays that are x, y co-ordinates and a square grid that corresponds to each point.

x = np.linspace(0,10,50)
y = np.linspace(-5,12,50)
square = np.empty((50,50))

I'm trying to plot out a square surface that is cmaped by the square values. I did get it to work with surface plotting but the issue is the surface has an irregular shape and I can't plot some scatter points over it, without visibility issues.

I'm wondering if there's a way to get it to work as a 2d plot rather than 3d as the Z cordinate can just be replaced by a cmap and make sense. Any suggestions?



Solution 1:[1]

So I did find a solution:

x_mesh, y_mesh = np.meshgrid(x,y)

fig = plt.figure(dpi = 200)
plt.scatter(x_mesh,y_mesh, c = square, cmap = 'viridis')
plt.show

With enough points it gets a good surface.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sleepy