【发布时间】:2015-03-30 11:39:36
【问题描述】:
是否可以使用 pyplot 的 triplot 函数绘制由 scipy.spatial.Delaunay 生成的三角形列表,以便可以绘制每个三角形并用单独的颜色填充?我创建的基本 python 脚本是
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
import matplotlib.image as mpimg
h = 300
w = 1000
npts = 30
pts = np.zeros((npts,2))
pts[:,0] = np.random.randint(0,w,npts)
pts[:,1] = np.random.randint(0,h,npts)
tri = Delaunay(pts)
plt.xlim(0, w)
plt.ylim(0, h)
# Determine the color used for each triangle based upon the orthocenter
# of the triangle and the corresponding pixel color in a background image.
centers = np.sum(pts[tri.simplices], axis=1, dtype='int')/3.0
colors = [img[y,x] for x,y in centers]
# This plots the edges of each triangle with no fill. I'd like to
# include the colors list to plot a fill for each.
plt.triplot(pts[:,0], pts[:,1], tri.simplices.copy())
plt.show()
triplot 是否有一些参数,我可以传递包含相应三角形颜色的颜色列表。我确信我可以使用适当的填充颜色循环绘制每个三角形,但如果有更优雅和更快的方法会更好。
【问题讨论】:
标签: python matplotlib delaunay