|
|
|||||||||||||||||||||||||||||
|
matplotlib mplot3d_Examples 29_wire3d_animation_demo. |
H.Kamifuji . |
3D プロットの非常に単純な「アニメーション」。 rotate_axes3d_demo も参照してください。
"""
==========================
Rotating 3D wireframe plot
==========================
A very simple 'animation' of a 3D plot. See also rotate_axes3d_demo.
"""
from __future__ import print_function
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import time
def generate(X, Y, phi):
'''
Generates Z data for the points in the X, Y meshgrid and parameter phi.
'''
R = 1 - np.sqrt(X**2 + Y**2)
return np.cos(2 * np.pi * X + phi) * R
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Make the X, Y meshgrid.
xs = np.linspace(-1, 1, 50)
ys = np.linspace(-1, 1, 50)
X, Y = np.meshgrid(xs, ys)
# Set the z axis limits so they aren't recalculated each frame.
ax.set_zlim(-1, 1)
# Begin plotting.
wframe = None
tstart = time.time()
for phi in np.linspace(0, 180. / np.pi, 500):
# If a line collection is already remove it before drawing.
if wframe:
ax.collections.remove(wframe)
# Plot the new wireframe and pause briefly before continuing.
Z = generate(X, Y, phi)
wframe = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2)
plt.pause(.001)
print('Average FPS: %f' % (100 / (time.time() - tstart)))
|
![]() Python 3.11.2 見直しました。上記のコードでは、下記のエラーが発生します。 Traceback (most recent call last): File "_:\wire3d_animation_demo.py", line 42, in <module> ax.collections.remove(wframe) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'ArtistList' object has no attribute 'remove' matplotlib 内部のエラーのようです。matplotlib の改修(先祖帰りバグの改修)を待つしかない。 Python 3.11.6 (matplotlib 3.7.1) では、下記のようなエラーがあり、実行できない。 Traceback (most recent call last): File "M:\______\wire3d_animation_demo.py", line 42, inPython 3.12.0 (matplotlib 3.8.1) では、下記のようなエラーがあり、実行できない。 Traceback (most recent call last): File "E:\______\wire3d_animation_demo.py", line 42, inPython 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) で、見直し中、新しいサンプル( wire3d-animation-sgskip-py ) を見つけ、下記のコードで、正常に実行できました。
"""
===========================
Animate a 3D wireframe plot
===========================
A very simple "animation" of a 3D plot. See also :doc:`rotate_axes3d_sgskip`.
(This example is skipped when building the documentation gallery because it
intentionally takes a long time to run.)
"""
import time
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
# Make the X, Y meshgrid.
xs = np.linspace(-1, 1, 50)
ys = np.linspace(-1, 1, 50)
X, Y = np.meshgrid(xs, ys)
# Set the z axis limits, so they aren't recalculated each frame.
ax.set_zlim(-1, 1)
# Begin plotting.
wframe = None
tstart = time.time()
for phi in np.linspace(0, 180. / np.pi, 100):
# If a line collection is already remove it before drawing.
if wframe:
wframe.remove()
# Generate data.
Z = np.cos(2 * np.pi * X + phi) * (1 - np.hypot(X, Y))
# Plot the new wireframe and pause briefly before continuing.
wframe = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2)
plt.pause(.001)
print('Average FPS: %f' % (100 / (time.time() - tstart)))
Python 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) 共に、正常実行です。![]() |
|
mplot3d_Examples code: wire3d_animation_demo.py wire3d-animation-sgskip-py |
|