matplotlib 2D plot from x,y,z values
<p>I am a Python beginner.</p>

<p>I have a list of X values </p>

<pre><code>x_list = [-1,2,10,3]
</code></pre>

<p>and I have a list of Y values</p>

<pre><code>y_list = [3,-3,4,7]
</code></pre>

<p>I then have a Z value for each couple. Schematically, this works like that:</p>

<pre><code>X   Y    Z
-1  3    5
2   -3   1
10  4    2.5
3   7    4.5
</code></pre>

<p>and the Z values are stored in <code>z_list = [5,1,2.5,4.5]</code>.
I need to get a 2D plot with the X values on the X axis, the Y values on the Y axis, and for each couple the Z value, represented by an intensity map.
This is what I have tried, unsuccessfully:</p>

<pre><code>X, Y = np.meshgrid(x_list, y_list) 
fig, ax = plt.subplots()
extent = [x_list.min(), x_list.max(), y_list.min(), y_list.max()]
im=plt.imshow(z_list, extent=extent, aspect = 'auto')
plt.colorbar(im)
plt.show()
</code></pre>

<p>How to get this done correctly?</p>

<p>The problem is that <code>imshow(z_list, ...)</code> will expect <code>z_list</code> to be an <code>(n,m)</code> type array, basically a grid of values. To use the imshow function, you need to have Z values for each grid point, which you can accomplish by collecting more data or interpolating. </p>

<p>Here is an example, using your data with linear interpolation:</p>

<pre><code>from scipy.interpolate import interp2d

# f will be a function with two arguments (x and y coordinates),
# but those can be array_like structures too, in which case the
# result will be a matrix representing the values in the grid 
# specified by those arguments
f = interp2d(x_list,y_list,z_list,kind="linear")

x_coords = np.arange(min(x_list),max(x_list)+1)
y_coords = np.arange(min(y_list),max(y_list)+1)
Z = f(x_coords,y_coords)

fig = plt.imshow(Z,
           extent=[min(x_list),max(x_list),min(y_list),max(y_list)],
           origin="lower")

# Show the positions of the sample points, just to have some reference
fig.axes.set_autoscale_on(False)
plt.scatter(x_list,y_list,400,facecolors='none')
</code></pre>

<p><a href="http://i.stack.imgur.com/wtKLd.png" rel="nofollow"><img src="http://i.stack.imgur.com/wtKLd.png" alt="enter image description here"></a></p>

<p>You can see that it displays the correct values at your sample points (specified by <code>x_list</code> and <code>y_list</code>, shown by the semicircles), but it has much bigger variation at other places, due to the nature of the interpolation and the small number of sample points.</p>

python matplotlib imshow