How to plot 3D data as a 2D mesh in Python?

I have 3D data as columns of a numpy array (ie array [0] = [0,0,0], etc.), for example

X     Y     Z

0     0     0
0     1     10
1     0     20
1     1     30

      

I would like to plot this so that each (X, Y) coordinate has a square centered on the coordinate, with a colored bar (for example) from 0 to 30 showing the Z value.

I would like to overlay some contour lines, but the first part of the question is the most important.

There is help for people who already have gridded data, but I'm not sure about the best matplotlib procedure to call my columns data. Also, this is for scientific publication, so it should be of good quality and look! Hope someone can help!

+3


source to share


1 answer


You can use griddata

from matplotlib.mlab

to set up your data correctly.

import numpy as np
from matplotlib.mlab import griddata

x = np.array([0,0,1,1])
y = np.array([0,1,0,1])
z = np.array([0,10,20,30])
xi = np.arange(x.min(),x.max()+1)
yi = np.arange(y.min(),y.max()+1)
ar = griddata(x,y,z,xi,yi)

# ar is now
# array([[  0.,  20.],
#        [ 10.,  30.]])

      



Selecting the displayed points xi

and yi

depends on you, and they do not necessarily have to be integers, as griddata

can interpolate for you.

+2


source







All Articles