Skip to content

Instantly share code, notes, and snippets.

@goddoe
Created May 17, 2020 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goddoe/a88ae0b68b3bfce0f6d7344d762ce868 to your computer and use it in GitHub Desktop.
Save goddoe/a88ae0b68b3bfce0f6d7344d762ce868 to your computer and use it in GitHub Desktop.
projection 3d points to sphere
# Reference: https://github.com/cvqluu/Angular-Penalty-Softmax-Losses-Pytorch/blob/master/plotting.py#L1
import matplotlib.pyplot as plt
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def plot(embeds, labels, fig_path='./example.pdf'):
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, projection='3d')
# Create a sphere
r = 1
pi = np.pi
cos = np.cos
sin = np.sin
phi, theta = np.mgrid[0.0:pi:100j, 0.0:2.0*pi:100j]
x = r*sin(phi)*cos(theta)
y = r*sin(phi)*sin(theta)
z = r*cos(phi)
ax.plot_surface(
x, y, z, rstride=1, cstride=1, color='w', alpha=0.3, linewidth=0)
ax.scatter(embeds[:,0], embeds[:,1], embeds[:,2], c=labels, s=20)
ax.set_xlim([-1, 1])
ax.set_ylim([-1, 1])
ax.set_zlim([-1, 1])
ax.set_aspect("equal")
plt.tight_layout()
plt.savefig(fig_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment