mardi 21 juin 2016

Plotting a wire frame sphere using MatPlotLib

Using MatPlotLib, I want to draw a sphere by plotting a set of spherical coordinates. I have got halfway there, but I cannot quite achieve what I want exactly.

So far, the following code:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

theta, phi = np.linspace(0, 2 * np.pi, 13), np.linspace(0, np.pi, 7)
THETA, PHI = np.meshgrid(theta, phi)
R = 1.0
X = R * np.sin(PHI) * np.cos(THETA)
Y = R * np.sin(PHI) * np.sin(THETA)
Z = R * np.cos(PHI)
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
plot = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, linewidth=0, color='white', antialiased=False, alpha=1, frameon=False)

plt.show()

Produces the following plot:

enter image description here

What I want to be able to do now, are the following two things:

  1. Stop shading each surface, so that all surfaces are the same colour (white). Instead of this, draw a line (black) between each surface. In this way, the sphere should appear like a wire frame, rather than a shaded solid object. I have tried shade=False in ax.plot_surface(...), and this gets me halfway by stopping the shading -- but all the surfaces end up being the same colour, and so without the wire frame, you cannot distinguish between each surface.

  2. Remove the axes from the plot, such that the sphere is just on its own. I have tried frameon=False in ax.plot_surface(...), but this gives me the error: AttributeError: Unknown property frameon.

Any help?

Thank you!

Aucun commentaire:

Enregistrer un commentaire