Skip to content

Instantly share code, notes, and snippets.

@jaredyam
Last active October 30, 2020 07:43
Show Gist options
  • Select an option

  • Save jaredyam/ac644dab576cbe08228481098ba117fc to your computer and use it in GitHub Desktop.

Select an option

Save jaredyam/ac644dab576cbe08228481098ba117fc to your computer and use it in GitHub Desktop.
"""Plot 3d figures with Python.
References
----------
1. https://stackoverflow.com/questions/8722735/i-want-to-use-matplotlib-to-make-a-3d-plot-given-a-z-function
2. https://stackoverflow.com/questions/31768031/plotting-points-on-the-surface-of-a-sphere-in-pythons-matplotlib
"""
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(131, projection='3d')
n = 10
xs = [i for i in range(n) for _ in range(n)]
ys = list(range(n)) * n
zs = [sum((x, y)) for x, y in zip(xs, ys)]
ax1.scatter(xs, ys, zs)
ax1.set_xlabel('X Label')
ax1.set_ylabel('Y Label')
ax1.set_zlabel('Z Label')
ax2 = fig.add_subplot(132, projection='3d')
x = y = np.arange(-3.0, 3.0, 0.05)
X, Y = np.meshgrid(x, y)
zs = np.array([sum((x, y)) for x, y in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)
ax2.plot_surface(X, Y, Z)
ax2.set_xlabel('X Label')
ax2.set_ylabel('Y Label')
ax2.set_zlabel('Z Label')
ax3 = fig.add_subplot(133, projection='3d')
r = 3
phi, theta = np.mgrid[0.0:np.pi:100j, 0.0:2.0 * np.pi:100j]
X = r * np.sin(phi) * np.cos(theta)
Y = r * np.sin(phi) * np.sin(theta)
Z = r * np.cos(phi)
ax3.plot_surface(X, Y, Z)
ax3.set_xlabel('X Label')
ax3.set_ylabel('Y Label')
ax3.set_zlabel('Z Label')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment