diff --git a/galleries/examples/mplot3d/rotate_axes3d_sgskip.py b/galleries/examples/mplot3d/rotate_axes3d_sgskip.py index 76a3369a20d6..df4fa730646b 100644 --- a/galleries/examples/mplot3d/rotate_axes3d_sgskip.py +++ b/galleries/examples/mplot3d/rotate_axes3d_sgskip.py @@ -5,7 +5,7 @@ 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) @@ -13,6 +13,7 @@ import matplotlib.pyplot as plt +from matplotlib import animation from mpl_toolkits.mplot3d import axes3d fig = plt.figure() @@ -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 @@ -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:: diff --git a/galleries/examples/mplot3d/wire3d_animation_sgskip.py b/galleries/examples/mplot3d/wire3d_animation.py similarity index 60% rename from galleries/examples/mplot3d/wire3d_animation_sgskip.py rename to galleries/examples/mplot3d/wire3d_animation.py index 903ff4918586..3104b35ffe8c 100644 --- a/galleries/examples/mplot3d/wire3d_animation_sgskip.py +++ b/galleries/examples/mplot3d/wire3d_animation.py @@ -4,9 +4,6 @@ =========================== 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 @@ -14,6 +11,11 @@ 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') @@ -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::