By default, plotting a set of points (or whatever) in 3D with matplotlib
, locates the z
axis vertically, as seen here (code below):
I need to interchange the z
and y
axis, so that that y
axis is shown vertically.
I've looked around but found no way to tell matplotlib
to do this.
Add: I do not want to have to resort to a hack where I interchange the data and labels. This is a simple 3 points 3D plot, but I have to plot much more complicated surfaces. I'm looking for a general solution, not just something that works with scatter plots. A simple way to tell matplotlib
to put the y
axis vertically instead of the z
axis is the clean way to do it.
MWE
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter([0.2, 0.5, 0.8], [2.3, 0.47, 1.], [2.1, 5.3, 0.7])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
Matplotlib invert x and y axisBy using invert_xaxis() and invert_yaxis() method we can flip the x-axis and y-axis respectively. In the above example, we import numpy and matplotlib libraries. After this, we define data on x and y coordinates. By using the plt.
Most common method is by using invert_xaxis() and invert_yaxis() for the axes objects. Other than that we can also use xlim() and ylim(), and axis() methods for the pyplot object. To invert X-axis and Y-axis, we can use invert_xaxis() and invert_yaxis() function.
To change the range of X and Y axes, we can use xlim() and ylim() methods.
I don't think that this is currently possible. ax.view_init()
would need to accept a third angle too.
I opened an issue over at github, https://github.com/matplotlib/matplotlib/issues/14453#issue-452397120, let's hope someone is committed to implement this feature.
Update
The third view angle was recently implemented and has been merged into the matplotlib main branch: https://github.com/matplotlib/matplotlib/pull/21426
It should appear in Matplotlib 3.6.0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With