我正在尝试使用 python3 和 matplotlib(版本 1.4.0)来绘制在球体表面上定义的标量函数。我想让面相对均匀地分布在球体上,所以我没有使用网格。这导致我使用plot_trisurf
来绘制我的函数。我已经用一个简单的标量函数对其进行了测试,并且遇到了沿着面部边缘存在渲染伪影的问题:
我用来创建绘图的代码如下:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.tri as mtri
from scipy.spatial import ConvexHull
def points_on_sphere(N):
""" Generate N evenly distributed points on the unit sphere centered at
the origin. Uses the 'Golden Spiral'.
Code by Chris Colbert from the numpy-discussion list.
"""
phi = (1 + np.sqrt(5)) / 2 # the golden ratio
long_incr = 2*np.pi / phi # how much to increment the longitude
dz = 2.0 / float(N) # a unit sphere has diameter 2
bands = np.arange(N) # each band will have one point placed on it
z = bands * dz - 1 + (dz/2) # the height z of each band/point
r = np.sqrt(1 - z*z) # project onto xy-plane
az = bands * long_incr # azimuthal angle of point modulo 2 pi
x = r * np.cos(az)
y = r * np.sin(az)
return x, y, z
def average_g(triples):
return np.mean([triple[2] for triple in triples])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = points_on_sphere(2**12)
Triples = np.array(list(zip(X, Y, Z)))
hull = ConvexHull(Triples)
triangles = hull.simplices
colors = np.array([average_g([Triples[idx] for idx in triangle]) for
triangle in triangles])
collec = ax.plot_trisurf(mtri.Triangulation(X, Y, triangles),
Z, shade=False, cmap=plt.get_cmap('Blues'), array=colors,
edgecolors='none')
collec.autoscale()
plt.show()
这个问题似乎已经在这个问题中讨论过,但我似乎无法弄清楚如何设置边缘颜色以匹配面部颜色。我尝试过的两件事是设置edgecolors='face'
和调用collec.set_edgecolors()
各种参数,但那些 throw AttributeError: 'Poly3DCollection' object has no attribute '_facecolors2d'
.
我应该如何在 trisurf 图中将边缘颜色设置为等于面颜色?