简体   繁体   English

使用matplotlib,Python进行表面绘图

[英]Surface plot with matplotlib, Python

I have a nx3 array, lets call it data , where I want the first 2 columns to be x and y coordinates and the 3rd column to be a z coordinate associated with the x and y coordinates in the same row. 我有一个nx3数组,我们称其为data ,在这里我希望前两列为xy坐标,第三列为与同一行中xy坐标关联的z坐标。

I now want to draw a surface plot where the surface intersects all the z coordinates. 我现在想绘制一个曲面图,其中曲面与所有z坐标相交。

I have seen this post but cannot figure it out. 我看过这篇文章,但无法弄清楚。

I know that I can use matplotlib's Axes3D and fig.gca(projection='3d') and that it takes 3 nxn arrays, where I think the X and Y arrays can be obtained with X,Y = np.meshgrid(data[:,0],data[:,1]) , but I am not sure how to obtain an nxn Z array if there is only 1 Z coordinate associated with each x and y . 我知道我可以使用matplotlib的Axes3Dfig.gca(projection='3d')并且它需要3个nxn数组,我认为XY数组可以通过X,Y = np.meshgrid(data[:,0],data[:,1]) ,但是如果每个xy只有1个Z坐标,则我不确定如何获取nxn Z数组。

Then, I would like to smoothen the surface, as I am sure a surface with only a few data points will look ugly, and I am only looking to represent the general shape of the data and specific values aren't too important. 然后,我想对表面进行平滑处理,因为我确信只有几个数据点的表面看起来很难看,而且我只是想代表数据的一般形状,而具体的值并不是太重要。 Thus, is there a way to interpolate between the data points in 2 dimensions to smoothen the graph? 因此,是否有一种方法可以在2维数据点之间进行插值以使图形平滑?

Example data set: 数据集示例:

data = np.array([[4260,150,116]
                 [4204,149,1070]
                 [4204,188,470]
                 [4444,140,389]
                 [3255,149,69]
                 [6370,149,1109]
                 [5765,189,3531]])

Try it like this: 像这样尝试:

x, y, z = data[:,0], data[:,1], data[:,2]
grid_x, grid_y = np.mgrid[min(x):max(x):50j, min(y):max(y):50j]
z = griddata((x, y), z, (grid_x, grid_y), method='cubic')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM