我正在尝试使用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()
这个问题似乎已在this question中讨论过,但我似乎无法弄清楚如何设置边缘颜色以匹配脸部颜色。我尝试过的两件事情是设置edgecolors='face'
并使用各种参数调用collec.set_edgecolors()
,但那些抛出AttributeError: 'Poly3DCollection' object has no attribute '_facecolors2d'
。
我应该如何在trisurf情节中将edgecolor设置为facecolor?
答案 0 :(得分:2)
您可以将antialiased
的{{1}}参数设置为plot_trisurf()
。结果如下: