rotating the z-axis in matplotlib 3d figure

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-28 20:18:55

问题


How do I rotate the z-axis of a matplotlib figure so that it appears as the horizontal axis? I have created the following 3d figure :

I tried adjusting the values in

ax.view_init() but no matter what values I used, I can't flip the z-axis so that the orientation of the three axes appear as like the ones in the following figure:

I thought of changing the three components of all the data so that I use the x-axis to plot the z-component of the data, but then it will be very inconvenient to many of my calculations, and more importantly, I don't know how to use ax.set_aspect to keep the whole figure in the same scale in a rectangular box (rectangular because the (original) z-component of the data usually spans a larger range than the x and y components).

So how do I rotate the z-axis (by 90 deg)?

EDIT:

Just to clarify my question. Using the example used in the answer, I have the following code that can generate a figure that has the SAME scale in all three axes, and even if I resize the pop-out window, the figure still has the same aspect ratio for the three axes and look the same. This is what I want, but with the z-axis rotated by 90deg so it appears horizontal (the desired coordinate system look like the one shown in the above figure of EM wave). If I switch the plotting order of the three axes, I do get a horizontal z-axis (which is plotted as the x-axis), but then I can't use the ax.set_aspect to change the scale.

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
ax.set_xlim(-30,30)
ax.set_ylim(-30,30)
ax.set_zlim(-90,90)
ax.set_aspect(3, 'box-forced')
plt.show()

回答1:


Can you just change the order of the X,Y,Z values that you pass to your plotting function?

Using this example:

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

plt.show()

Changing the input order:

ax.plot_wireframe(X, Z, Y, rstride=10, cstride=10)



来源:https://stackoverflow.com/questions/34981479/rotating-the-z-axis-in-matplotlib-3d-figure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!