matplotlib 如何旋转3D打印

qv7cva1a  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(159)

我用matplotlib写了一个3D代码。但是,我不知道如何旋转的情节?
我试过https://matplotlib.org/stable/gallery/mplot3d/rotate_axes3d_sgskip.html中的代码,但它不旋转。有什么帮助吗?

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

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

# Grab some example data and plot a basic wireframe.
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

# Set the axis labels
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

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

    # Cycle through a full rotation of elevation, then azimuth, roll, and all
    elev = azim = roll = 0
    if angle <= 360:
        elev = angle_norm
    elif angle <= 360*2:
        azim = angle_norm
    elif angle <= 360*3:
        roll = angle_norm
    else:
        elev = azim = roll = angle_norm

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

    plt.draw()
    plt.pause(.001)
xurqigkl

xurqigkl1#

尝试使用view_init,这里有一个例子:

import matplotlib.pyplot as plt
import numpy as np

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

# Generate some data using NumPy
X = np.linspace(-5, 5, 100)
Y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(X, Y)
Z = np.sin(np.sqrt(X ** 2 + Y ** 2))

# Plot the surface
ax.plot_surface(X, Y, Z, cmap='viridis')

# Set the azimuth and elevation angles
ax.view_init(azim=45, elev=30)

plt.show()

相关问题