python - 如何使用plot_trisurf

标签 python matplotlib mplot3d

将matplotlib版本从1.3.1更新到2.0.2后,当我想使用plot_trisurf通过3d点生成TIN时,我得到了难以理解的结果。我的测试代码如下:

import sys
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy
from numpy.random import randn
from scipy import array, newaxis

chunk = numpy.loadtxt('test.xyz')  #test.xyz contains 38010 points,
DATA=numpy.array(chunk)
Xs = DATA[:,0]
Ys = DATA[:,1]
Zs = DATA[:,2]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

surf = ax.plot_trisurf(Xs, Ys, Zs, cmap=cm.jet, linewidth=0)
fig.colorbar(surf)

ax.xaxis.set_major_locator(MaxNLocator(5))
ax.yaxis.set_major_locator(MaxNLocator(6))
ax.zaxis.set_major_locator(MaxNLocator(5))

fig.tight_layout()
plt.show()

test.xyz 文件包含 38010 个点。部分内容如下,完整文件见here .

512743.63 5403547.33 308.68
512743.62 5403547.33 308.70
512743.61 5403547.33 308.72
512743.60 5403547.34 308.68
512743.60 5403547.33 308.73
512741.50 5403547.36 309.05
512741.50 5403547.36 309.07
512741.49 5403547.46 309.09
512741.48 5403547.46 309.07
512741.47 5403547.46 309.10
512741.47 5403547.45 309.13
512741.46 5403547.37 309.04
512739.39 5403547.51 309.10
512739.39 5403547.48 309.34
512739.38 5403547.60 309.25
512739.37 5403547.71 309.15
512739.39 5403547.49 310.65
512739.39 5403547.48 310.70
512739.38 5403547.49 310.69
512739.37 5403547.48 310.72
512739.36 5403547.39 310.64
512739.32 5403547.41 309.20
512737.33 5403547.26 313.14
512737.33 5403547.37 313.09
512737.32 5403547.38 313.03
512737.30 5403547.37 313.12
512737.30 5403547.26 313.14
512735.22 5403547.41 311.72
512735.22 5403547.43 312.29
512735.22 5403547.49 312.59
512735.21 5403547.51 312.48
512735.20 5403547.60 312.53
512735.19 5403547.61 312.48
512735.18 5403547.72 312.40
512735.18 5403547.71 312.49
512735.17 5403547.71 312.51
512735.16 5403547.70 312.58
512735.15 5403547.61 312.52

更新后结果显示为: ]

我认为这是错误的,因为我提供了足够的点来生成TIN,但结果似乎只使用了一小部分点。在更新 matplotlib 之前,我可以获得如下结果: ]

最佳答案

非常感谢大家的回复。该问题已解决,详细信息参见Problem about plot_trisurf of matplotlib 2.0.2 。 在这里我很高兴展示我的结果。 问题是在 qhull 中计算 Delaunay 三角剖分时的有限精度问题之一,它认为附近的点(根据“附近”一词的复杂定义)是相同的,因此三角剖分比期望的更简单。对于有限精度,该数据集是一个极端的数据集(以一种不好的方式),因为点的均值分布很小(x.mean()=512767、x.max()-x.min()=134、y .mean()=303, y.max()-y.min()=5403707)。 Ian Thomas 解释了这一点。 因此,我更正了我的测试代码如下:

import sys
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import numpy
from numpy.random import randn
from scipy import array, newaxis

chunk = numpy.loadtxt('test.xyz')  #test.xyz contains 38010 points,
DATA=numpy.array(chunk)
Xs = DATA[:,0]
Ys = DATA[:,1]
Zs = DATA[:,2]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
#surf = ax.plot_trisurf(Xs, Ys, Zs, cmap=cm.jet, linewidth=0)
surf = ax.plot_trisurf(Xs-Xs.mean(), Ys-Ys.mean(), Zs, cmap=cm.jet, linewidth=0)
fig.colorbar(surf)

ax.xaxis.set_major_locator(MaxNLocator(5))
ax.yaxis.set_major_locator(MaxNLocator(6))
ax.zaxis.set_major_locator(MaxNLocator(5))

fig.tight_layout()
plt.show()

之前的结果是这样的: enter image description here

执行后结果如下: enter image description here

总而言之,这实际上并不是不同 matplotlib 版本之间的问题,当前版本足以应对大多数用例。如果有人希望轴刻度标签可以轻松纠正,您可以引用 ImportanceOfBeingErnest 的 method.

关于python - 如何使用plot_trisurf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44244297/

相关文章:

python - 不使用 shell-True 提取帧数

python - 将 matplotlib 动画嵌入到 tkinter 框架中

c++ - 在 C++ Qt 应用程序中嵌入 Python/Numpy/Matplotlib?

python - 如何以 3D 形式绘制 2 个向量

python - 在 3D 表面上绘制非均匀数据

python - Django REST 框架 : Customize data dict on JSONRenderer

Python 3.2.3 值错误 : could not convert string to float

python - 在 python 中使用 [::-1] 来反转列表 O(1) 空间吗?

python - 在 python 中使用 matplotlib 绘制对数轴

python - Matplotlib 中具有互连的堆叠二维图