Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions galleries/examples/mplot3d/rotate_axes3d_sgskip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

A very simple animation of a rotating 3D plot about all three axes.

See :doc:`wire3d_animation_sgskip` for another example of animating a 3D plot.
See :doc:`wire3d_animation` for another example of animating a 3D plot.

(This example is skipped when building the documentation gallery because it
intentionally takes a long time to run)
"""

import matplotlib.pyplot as plt

from matplotlib import animation
from mpl_toolkits.mplot3d import axes3d

fig = plt.figure()
Expand All @@ -27,8 +28,9 @@
ax.set_ylabel('y')
ax.set_zlabel('z')


# Rotate the axes and update
for angle in range(0, 360*4 + 1):
def animate(angle):
# Normalize the angle to the range [-180, 180] for display
angle_norm = (angle + 180) % 360 - 180

Expand All @@ -45,10 +47,12 @@

# Update the axis view and title
ax.view_init(elev, azim, roll)
plt.title('Elevation: %d°, Azimuth: %d°, Roll: %d°' % (elev, azim, roll))
ax.set_title(f'Elevation: {elev}°, Azimuth: {azim}°, Roll: {roll}°')


ani = animation.FuncAnimation(fig, animate, interval=25, frames=360*4)

plt.draw()
plt.pause(.001)
plt.show()

# %%
# .. tags::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
===========================

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

from matplotlib import animation

FRAMES = 25
FPS = 25

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

Expand All @@ -28,17 +30,28 @@
# 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.


def animate(i):
global wframe
# If a line collection is already there, remove it before drawing.
if wframe:
wframe.remove()
# Generate data.
phi = i / FRAMES * 2 * np.pi
Z = np.cos(2 * np.pi * X + phi) * (1 - np.hypot(X, Y))
# Plot the new wireframe and pause briefly before continuing.
# Plot the new wireframe.
wframe = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2)
plt.pause(.001)
if i == FRAMES - 1: # Print FPS at the end of the loop.
global tstart
fps = FRAMES / (time.time() - tstart)
print(f'Expected FPS: {FPS}; Average FPS: {fps}')
tstart = time.time()


ani = animation.FuncAnimation(fig, animate, interval=1000 / FPS, frames=FRAMES)

print('Average FPS: %f' % (100 / (time.time() - tstart)))
plt.show()

# %%
# .. tags::
Expand Down
Loading