Skip to content

[ENH]: Provide axis('equal') for Axes3D. #22570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
patquem opened this issue Feb 28, 2022 · 4 comments · Fixed by #23409
Closed

[ENH]: Provide axis('equal') for Axes3D. #22570

patquem opened this issue Feb 28, 2022 · 4 comments · Fixed by #23409

Comments

@patquem
Copy link
Contributor

patquem commented Feb 28, 2022

Bug summary

In the example below, I wish to observe a unitary 3D cube in its diagonal axis (1, 1, 1).
azimut and elevation angle should be both 45°, normally.
Why do I need to set elevation = 28° ????
Thanks,
Patrick

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 3, figsize=(15, 5),
                       subplot_kw=dict(projection="3d",
                                       proj_type='ortho'))

ax[0].voxels(np.ones((1, 1, 1)), facecolors=[0, 0, 0, 0], edgecolors='k')
ax[0].quiver(0, 0, 0, 1, 1, 1, color='r', linewidth=5)

ax[1].set_title("azim=45°, elev=45°")
ax[1].voxels(np.ones((1, 1, 1)), facecolors=[0, 0, 0, 0], edgecolors='k')
ax[1].view_init(azim=45, elev=45)
ax[1].quiver(0, 0, 0, 1, 1, 1, color='r', linewidth=5)

ax[2].set_title("azim=45°, elev=28°")
ax[2].voxels(np.ones((1, 1, 1)), facecolors=[0, 0, 0, 0], edgecolors='k')
ax[2].view_init(azim=45, elev=28)
ax[2].quiver(0, 0, 0, 1, 1, 1, color='r', linewidth=5)

plt.show()

Actual outcome

image

Expected outcome

view_init(azim=45, elev=45) and not view_init(azim=45, elev=28) to have the expected 3D representation

Additional information

No response

Operating system

Windows

Matplotlib Version

3.4.2

Matplotlib Backend

No response

Python version

Python 3.7.7

Jupyter version

No response

Installation

pip

@oscargus
Copy link
Member

oscargus commented Feb 28, 2022

Ideally, it should be possible to do ax[1].axis('equal') to obtain what you are looking for (see https://matplotlib.org/stable/gallery/subplots_axes_and_figures/axis_equal_demo.html). However, Axes3D does not (yet) support that:

Traceback (most recent call last):

  Input In [18] in <module>
    ax[1].axis('equal')

  File /local/data1/miniconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py:2047 in axis
    self.set_aspect('equal', adjustable='datalim')

  File /local/data1/miniconda3/lib/python3.8/site-packages/mpl_toolkits/mplot3d/axes3d.py:311 in set_aspect
    raise NotImplementedError(

NotImplementedError: Axes3D currently only supports the aspect argument 'auto'. You passed in 'equal'.

So, basically, there is a different scaling for the z-axis compared to the x- and y-axes. You can see that from the grid blocks being rectangles (rather than squares, as for the bottom surface).

@oscargus oscargus changed the title [Bug]: elevation angle not correct with view_init() ? [ENH]: Provide axis('equal') for Axes3D. Feb 28, 2022
@oscargus
Copy link
Member

You can get a bit closer by using: ax[1].set_box_aspect((4, 4, 4)), but it is still not exact. (Of course, you can probably find a better value for the last number, but that is about as bad as 28 degrees...) Now it at least looks like a cube.

@patquem
Copy link
Contributor Author

patquem commented Feb 28, 2022

Hi @oscargus,
Thanks for your reply.
using ax[1].set_box_aspect((1, 1, 1)), the elevation angle is now 35°.
That is the correct elevation angle (atan(1/sqrt(2) ~35°) and not 45° as I have previously written.
Thanks.

@jondo
Copy link

jondo commented Aug 2, 2022

B.t.w., this is a successor of #1077.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment