0

I am trying to use python3 and matplotlib (version 1.4.0) to plot a scalar function defined on the surface of a sphere. I would like to have faces distributed relatively evenly over the sphere, so I am not using a meshgrid. This has led me to use plot_trisurf to plot my function. I have tested it with a trivial scalar function, and am having the problem that there are rendering artefacts along the edges of the faces: Edge artefacts The code I used to create the plot is below:

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 problem appears to have been discussed in this question, but I can't seem to figure out how to set the edgecolors to match the facecolors. The two things I've tried are setting edgecolors='face' and calling collec.set_edgecolors() with a variety of arguments, but those throw AttributeError: 'Poly3DCollection' object has no attribute '_facecolors2d'.

How am I supposed to set the edgecolor equal to the facecolor in a trisurf plot?

1 Answers1

2

You can set antialiased argument of plot_trisurf() to False. Here is the result:

enter image description here

HYRY
  • 94,853
  • 25
  • 187
  • 187
  • 1
    That does solve the problem of the artefacts on edges. It would be nice if there were a way to solve the problem without disabling anti-aliasing, which I assume setting the edge colors appropriately is supposed to do. If no one is able to come up with a way to set the edge colors to match the face colors in `plot_trisurf`, though, I will happily accept this answer. – Jonathan A. Gross Nov 05 '14 at 16:39