python scatter 三维曲面,剧情从{X,Y,Z} 3D表面-scatter在Python数据

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:

IEgG5.png

Compare to the result from ListSurfacePlot3D:

JJjQP.png

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:

cULvz.png

and using plot_wireframe:

9zQhf.png

and using ListSurfacePlot3D:

NPXHe.png

解决方案

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

wdxx3.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要进行三维曲面拟合,可以使用Python中的SciPy库中的`scipy.optimize.curve_fit()`函数。这个函数可以用来拟合任何形式的函数,包括三维曲面。 首先,需要导入必要的库: ```python import numpy as np from scipy.optimize import curve_fit ``` 然后,可以定义一个函数来表示你要拟合的三维曲面模型。例如,如果你想拟合一个二次曲面,可以定义如下的函数: ```python def func(x, y, a, b, c, d, e, f): return a + b*x + c*y + d*x**2 + e*x*y + f*y**2 ``` 其中,`x`和`y`是自变量,`a`到`f`是拟合参数。这个函数可以根据你的需要进行修改。 接下来,需要准备数据。假设你有一个三维数据集,其中`x`、`y`和`z`分别表示自变量和因变量: ```python x = np.array([1, 2, 3, 4, 5]) y = np.array([6, 7, 8, 9, 10]) z = np.array([0.1, 0.5, 1.2, 2.1, 3.5]) ``` 然后,可以使用`curve_fit()`函数进行拟合: ```python popt, pcov = curve_fit(func, (x, y), z) ``` 其中,`popt`是拟合参数的最优值,`pcov`是协方差矩阵。可以使用这些参数来绘制拟合曲面。 ```python import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # 绘制原始数据点 ax.scatter(x, y, z) # 生成拟合曲面的网格点 x_surf = np.linspace(min(x), max(x), 100) y_surf = np.linspace(min(y), max(y), 100) x_surf, y_surf = np.meshgrid(x_surf, y_surf) z_surf = func(x_surf, y_surf, *popt) # 绘制拟合曲面 ax.plot_surface(x_surf, y_surf, z_surf) plt.show() ``` 这样就可以得到一个拟合曲面三维图像了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值