Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib 2D plot from x,y,z values

I am a Python beginner.

I have a list of X values

x_list = [-1,2,10,3]

and I have a list of Y values

y_list = [3,-3,4,7]

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

X   Y    Z
-1  3    5
2   -3   1
10  4    2.5
3   7    4.5

and the Z values are stored in z_list = [5,1,2.5,4.5]. 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:

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

How to get this done correctly?

like image 928
johnhenry Avatar asked Sep 27 '16 14:09

johnhenry


People also ask

How to Plot X Y and z data points in Python?

Plot x, y and z data points using plot_surface () method. To display the figure, use show () method.

How to plot list of X and Y coordinates in Matplotlib?

How to Plot List of X, Y Coordinates in Matplotlib? 1 Method 1: Naive method. In this method, the coordinates are simply passed as two lists. 2 Method 2: Using numpy array. Step: step size, by default it is 1. 3 Method 3: List of lists. Creating lists of list of all points to be plotted can also one of the ways of achieving our... More ...

How do you plot a subplot in Matplotlib?

Set the figure size and adjust the padding between and around the subplots. Create a new figure or activate an existing figure using figure () method. Add an axes to the figure as part of a subplot arrangement. Create x, y, X, Y and Z data points using numpy. Plot x, y and z data points using plot_surface () method.

Is it bad if my data is missing points in Matplotlib?

It’s not detrimental if your data don’t meet this requirement, but you may get unwanted blank spots in your plot if your data is missing any points in the plane. Given data in this format, we can quickly convert it to the requisite structure for matplotlib using the code below.


Video Answer


1 Answers

The problem is that imshow(z_list, ...) will expect z_list to be an (n,m) 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.

Here is an example, using your data with linear interpolation:

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

enter image description here

You can see that it displays the correct values at your sample points (specified by x_list and y_list, 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.

like image 154
Albert P Avatar answered Oct 05 '22 04:10

Albert P