Skip to content

Instantly share code, notes, and snippets.

@moble
Created February 25, 2016 16:22
Show Gist options
  • Save moble/5a6b0c852674f1bff550 to your computer and use it in GitHub Desktop.
Save moble/5a6b0c852674f1bff550 to your computer and use it in GitHub Desktop.
"""
Example showing buggy behavior in matplotlib's z-ordering for 3D objects
This example draws a sphere of radius 0.9, and a ring around it at radius 1.0. The ring should therefore be completely
outside of the sphere, and when viewed from above should be completely visible. However, depending on the elevation of the
viewpoint, the ring becomes hidden. In particular, the default elevation shows this problem. However, any elevation of
48.02450 and above seems to work just fine, but anything below that doesn't work.
"""
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection="3d")
# Draw a sphere
θ = np.linspace(0, np.pi)
ϕ = np.linspace(0, 2*np.pi)
θ, ϕ = np.meshgrid(θ, ϕ)
r_sphere = 0.9
X = r_sphere * np.sin(θ) * np.cos(ϕ)
Y = r_sphere * np.sin(θ) * np.sin(ϕ)
Z = r_sphere * np.cos(θ)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, color='g')
# Draw a circle around it
θ = np.pi/8
ϕ = np.linspace(0, 2*np.pi)
r_circle = 1.0
x = r_circle * np.sin(θ) * np.cos(ϕ)
y = r_circle * np.sin(θ) * np.sin(ϕ)
z = r_circle * np.cos(θ)
ax.scatter(x, y, z)
# Set the elevation
# ax.elev = 48.02449 # Doesn't work
# ax.elev = 48.02450 # Works
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment