Skip to content

Instantly share code, notes, and snippets.

@yangyushi
Created August 24, 2020 15:50
Show Gist options
  • Save yangyushi/061fe2f585e266803ce6cdfbd51aaf8d to your computer and use it in GitHub Desktop.
Save yangyushi/061fe2f585e266803ce6cdfbd51aaf8d to your computer and use it in GitHub Desktop.
draw 50 fish in 3D using Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import mpl_toolkits.mplot3d.art3d as art3d
positions = np.random.random((3, 50)) * 10
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
fish_vertices = np.array([
(0, 0), (1, 2), (5, 2),
(7, 0), (8, -1), (7.8, 0), (8, 1), (7, 0),
(5, -1), (1, -1.5), (0, 0)
]) / 5
codes = [1, 4, 4, 4, 2, 2, 2, 2, 4, 4, 4]
for p in positions.T:
v = fish_vertices + p[np.newaxis, :2]
fish = PathPatch(
Path(v, codes=codes),
facecolor='tomato',
edgecolor='k'
)
ax.add_patch(fish)
art3d.pathpatch_2d_to_3d(
fish, z=p[-1], zdir="y"
)
ax.set_xlim(-2, 12)
ax.set_ylim(-2, 12)
ax.set_zlim(-2, 12)
plt.show()
@yangyushi
Copy link
Author

Here is what I got with the code

fish

@yangyushi
Copy link
Author

Major breakthrough of my PhD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment