从python中的{x,y,z}-散点数据绘制3D表面 [英] Plot a 3D surface from {x,y,z}-scatter data in python

查看:59
本文介绍了从python中的{x,y,z}-散点数据绘制3D表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一个 3D 曲面,该曲面构造为适合 Python 中的一些 {x,y,z} 点 - 理想情况下类似于 Mathematica 发布的 test.csv 文件:

导入csv从 matplotlib 导入 pyplot导入pylab从 mpl_toolkits.mplot3d 导入 Axes3DhFile = open("test.csv", 'r')datfile = csv.reader(hFile)数据 = []对于 datfile 中的行:dat.append(map(float,row))温度 = zip(*(dat))图 = pylab.figure(figsize=pyplot.figaspect(.96))ax = Axes3D(图)

那么,要么

ax.plot_surface(temp[0], temp[1], temp[2])pyplot.show()

ax.plot_wireframe(temp[0], temp[1], temp[2])pyplot.show()

这是使用 plot_surface 呈现的方式:并使用 plot_wireframe:并使用 ListSurfacePlot3D:

解决方案

plot_surface 需要二维数组形式的 X、Y、Z 值,正如 np.meshgrid 返回的那样.当输入以这种方式有规律地网格化时,绘图函数隐式地知道表面中的哪些顶点彼此相邻,因此应该与边连接.但是,在您的示例中,您正在处理一维坐标向量,因此绘图函数需要能够确定应连接哪些顶点.

plot_trisurf 函数确实通过进行 Delaunay 三角剖分来确定哪些点应该与边相连以避免细三角形",从而处理不规则间隔的点:

I'm trying to plot a 3D surface constructed to fit some {x,y,z} points in python -- ideally something like the Mathematica ListSurfacePlot3D function. Thus far I've tried plot_surface and plot_wireframe on my points to no avail.

Only the axes render with plot_surface. plot_wireframe gives a bunch of squigglys, vaguely in the shape of the object, but not the nice sort that is shown in the documentation: Compare to the result from ListSurfacePlot3D:

Here is a minimal working example, using a test.csv file I posted here:

import csv
from matplotlib import pyplot
import pylab
from mpl_toolkits.mplot3d import Axes3D

hFile = open("test.csv", 'r')
datfile = csv.reader(hFile)
dat = []

for row in datfile:
        dat.append(map(float,row))

temp = zip(*(dat))

fig = pylab.figure(figsize=pyplot.figaspect(.96))
ax = Axes3D(fig)

Then, either

ax.plot_surface(temp[0], temp[1], temp[2])
pyplot.show()

or

ax.plot_wireframe(temp[0], temp[1], temp[2])
pyplot.show()

This is how it renders using plot_surface: and using plot_wireframe: and using ListSurfacePlot3D:

解决方案

plot_surface expects X,Y,Z values in the form of 2D arrays, as would be returned by np.meshgrid. When the inputs are regularly gridded in this way, the plot function implicitly knows which vertices in the surface are adjacent to one another and therefore should be joined with edges. In your example, however, you're handing it 1D vectors of coordinates, so the plotting function would need to be able to figure out which vertices should be joined.

The plot_trisurf function does handle irregularly spaced points by doing a Delaunay triangulation to determine which points should be joined with edges in such a way as to avoid 'thin triangles':

这篇关于从python中的{x,y,z}-散点数据绘制3D表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆