Skip to content

Instantly share code, notes, and snippets.

@NorimasaNabeta
Created July 19, 2020 19:49
Show Gist options
  • Save NorimasaNabeta/fb69dd28baff5038f1199a6fb196c6d0 to your computer and use it in GitHub Desktop.
Save NorimasaNabeta/fb69dd28baff5038f1199a6fb196c6d0 to your computer and use it in GitHub Desktop.
# -*- mode: python; coding: utf-8-unix -*-
#
# Time-stamp: <2020-07-20 04:47:17 norim>
#
# @ref: https://nathankjer.com/animated-3d-plots/
#
# CAUTION: required to install the imagemagick before execution.
# for jupyter, http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-notebooks/
#
import time
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from mpl_toolkits.mplot3d import Axes3D
matplotlib.rcParams['animation.embed_limit'] = 2**128
# @brief performance counter timer.
start_time = time.perf_counter()
fig = plt.figure()
fig.subplots_adjust(left=0, bottom=0, right=1, top=1)
ax = fig.add_subplot(111, projection='3d')
ax.set_facecolor((0.5, 0.5, 0.5))
## @brief modified
#gradient = np.linspace(0, 1, 2)
#X,Y,Z = np.meshgrid(gradient, gradient, gradient)
#colors=np.stack((X.flatten(),Y.flatten(),Z.flatten()),axis=1)
#ax.scatter(X,Y,Z,alpha=1.0,s=50,c=colors,marker='o',linewidth=0)
# bits=8 # too big
bits=2
gradient = np.linspace(0,1,2**bits)
X,Y,Z = np.meshgrid(gradient, gradient, gradient)
colors=np.stack((X.flatten(),Y.flatten(),Z.flatten()),axis=1)
ax.scatter(X,Y,Z,alpha=1.0,s=100./2**bits,c=colors,marker='o',linewidth=0)
## <<
plt.axis('off')
fig.set_size_inches(5, 5)
def update(i, fig, ax):
ax.view_init(elev=20., azim=i)
return fig, ax
anim = FuncAnimation(fig, update, frames=np.arange(0, 360, 2), repeat=True, fargs=(fig, ax))
plt.show()
#anim.save('rgb_cube.gif', dpi=80, writer='imagemagick', fps=24)
# (base) D:\opt\motion_detector>python animation-3d-plots.py
# MovieWriter imagemagick unavailable; trying to use pillow instead.
# Traceback (most recent call last):
# File "animation-3d-plots.py", line 33, in <module>
# anim.save('rgb_cube.gif', dpi=80, writer='imagemagick', fps=24)
# File "D:\opt\anaconda3\lib\site-packages\matplotlib\animation.py", line 1111, in save
# extra_args=extra_args, metadata=metadata)
# TypeError: 'str' object is not callable
# C:\Program Files\ImageMagick-7.0.10-Q16-HDRI\ffmpeg
# C:\Program Files\iSpy\ffmpeg
# to solve this problem, set the valid path of ffmpeg.exe to the environment variable PATH.
# writer : :class:`MovieWriter` or str, optional
# A `MovieWriter` instance to use or a key that identifies a
# class to use, such as 'ffmpeg'. If ``None``, defaults to
# :rc:`animation.writer` = 'ffmpeg'.
#anim.save('rgb_cube_bits2.mp4', dpi=80, writer='ffmpeg', fps=24)
# (base) D:\opt\motion_detector>python animation-3d-plots.py
# MovieWriter ffmpeg unavailable; trying to use pillow instead.
# Traceback (most recent call last):
# File "animation-3d-plots.py", line 37, in <module>
# anim.save('rgb_cube.mp4', dpi=80, writer='ffmpeg', fps=24)
# File "D:\opt\anaconda3\lib\site-packages\matplotlib\animation.py", line 1111, in save
# extra_args=extra_args, metadata=metadata)
# TypeError: 'str' object is not callable
# @brief performance result print
print (time.perf_counter() - start_time, "seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment