3

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 : enter image description here

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: enter image description here

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()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Physicist
  • 2,848
  • 8
  • 33
  • 62
  • Does this answer your question? [Interchange location of y and z axis in 3D matplotlib plot](https://stackoverflow.com/questions/34812843/interchange-location-of-y-and-z-axis-in-3d-matplotlib-plot) – bluenote10 Apr 21 '21 at 18:56

1 Answers1

0

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()

enter image description here

Changing the input order:

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

enter image description here

Lee
  • 29,398
  • 28
  • 117
  • 170
  • it works. the problem is that I want to keep the scale of my figure the same in all 3 axes. FOr example, in the figure I attached, the data spans a larger range in the z-direction by 2.5 times, so I just use ax.set_aspect and set 2.5 as the ratio. If I change the input order to ax.plot(z,x,y), then how do I keep the scale unchanged (which means the now the x-axis has to be longer) – Physicist Jan 25 '16 at 13:25
  • I tried the ax.auto_scale_xyz, but still it doesn't fix the aspect ratio, so I can still change the scale of the figure by simply changing the size of the pop-out window. If I use the set_aspect, then I am just fixing the ratio of the z-axis and the x-y plane. This works if I don't switch the plot order, but once I switch the plot order as you suggested, then it is not changing the ratio I want. Now I need to x-axis to appear 2.5x longer than y and z-axis, and keep the aspect ratio of the whole figure unchanged all the time. – Physicist Jan 25 '16 at 13:39
  • @Physicist Perhaps you could provide [some code](http://stackoverflow.com/help/mcve), in a new question if necessary. – Lee Jan 25 '16 at 13:51
  • I edited the question. In your example, the z-axis is 'compressed'. I want all three axes to have the same scale, even if I resize the pop-out windoe, but with the z-axis rotated to the horizontal direction. – Physicist Jan 25 '16 at 20:30