with matplotlib (and ax.view_init(elem, azim) ? ) how to rotate by 90 degrees on the x,y…
- User
with matplotlib (and ax.view_init(elem, azim) ? ) how to rotate by 90 degrees on the x,y axis (so z is the pivot axis)
- Assistant
In matplotlib, you can use ax.view_init() to rotate a 3D plot. To rotate the plot by 90 degrees around the Z-axis (which keeps the Z-axis fixed and rotates the X and Y axes), you can adjust the azim (azimuth) parameter in ax.view_init(). Here's an example: pythonCopiarEditarimport matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np
Create a 3D figure
fig = plt.figure() ax = fig.add_subplot(111, projection='3d')
Sample data
x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) z = np.sin(np.sqrt(x2 + y2))
Plot data
ax.plot(x, y, z)
Rotate by 90 degrees on the XY-plane (Z-axis is the pivot)
ax.view_init(elev=10, azim=90) # Adjust azim=90 for the 90-degree rotation
Show plot
plt.show()
In this example:
azim=90 rotates the view by 90 degrees around the Z-axis (so the X and Y axes rotate).
elev=10 sets the elevation angle, which controls the vertical view angle.
Adjust the elev and azim parameters to achieve your desired view angle!