【问题标题】:Using scipy.spatial.Delaunay in place of matplotlib.tri.Triangulation's built-in version使用 scipy.spatial.Delaunay 代替 matplotlib.tri.Triangulation 的内置版本
【发布时间】:2014-05-04 10:33:03
【问题描述】:

似乎matplotlib.tri.Triangulation 使用了一个错误且可能不正确的 Delaunay 三角剖分实现,该实现将由qHull 替换。

我正在尝试使用 mpl_toolkits.mplot3d.plot_trisurf() 绘制一个 trisurf 并遇到一堆无用的异常(IndexErrors 和 KeyErrors 主要是,没有迹象表明到底出了什么问题)。

由于scipy.spatial.Delaunay 已经使用 qHull,我想知道是否有一种方法可以使用 scipy 的 Delaunay 三角测量来构建一个 matplotlib.tri.Triangulation 对象以与 mpl_toolkits.mplot3d.plot_trisurf() 一起使用。

我尝试通过triangles 参数将delaunay.points 直接传递给matplotlib.tri.Triangulate,但这会导致ValueError: triangles min element is out of bounds

【问题讨论】:

  • 另一方面,如果您可以尝试使用当前的 master 分支,这将在 1.4 中修复。

标签: python matplotlib scipy delaunay


【解决方案1】:

http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.spatial.Delaunay.html http://matplotlib.org/dev/api/tri_api.html

所以你需要将点和三角形从 qhull 传递给 Triangulation 构造函数:

import numpy as np
import scipy.spatial
import matplotlib
import math

import matplotlib.tri as mtri
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# First create the x and y coordinates of the points.
n_angles = 20
n_radii = 10
min_radius = 0.15
radii = np.linspace(min_radius, 0.95, n_radii)
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += math.pi/n_angles
x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()

# Create the Delaunay tessalation using scipy.spatial
pts = np.vstack([x, y]).T
tess = scipy.spatial.Delaunay(pts)

# Create the matplotlib Triangulation object
x = tess.points[:, 0]
y = tess.points[:, 1]
tri = tess.vertices # or tess.simplices depending on scipy version
triang = mtri.Triangulation(x=pts[:, 0], y=pts[:, 1], triangles=tri)

# Plotting
z = x*x + y*y
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(triang, z)
plt.show()

输出(使用 matplotlib 当前主控):

【讨论】:

  • 如果我只有一个二维数组作为输入?
猜你喜欢
  • 2017-08-07
  • 2016-02-23
  • 1970-01-01
  • 2014-05-29
  • 2019-06-25
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多