Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interchange location of y and z axis in 3D matplotlib plot

By default, plotting a set of points (or whatever) in 3D with matplotlib, locates the z axis vertically, as seen here (code below):

enter image description here

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()
like image 339
Gabriel Avatar asked Jan 15 '16 14:01

Gabriel


People also ask

How do you reverse the axis of a 3d plot in Python?

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.

How do you flip the axis in Pyplot?

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.

How do I change the Y axis scale in matplotlib?

To change the range of X and Y axes, we can use xlim() and ylim() methods.


1 Answers

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.

like image 173
Bastian Avatar answered Oct 02 '22 22:10

Bastian