Skip to content
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

[BUG]: Shift box_aspect according to vertical_axis #28041

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

Illviljan
Copy link
Contributor

@Illviljan Illviljan commented Apr 7, 2024

PR summary

Make sure box_aspect stays the same for each axis when using set_box_aspect and vertical_axis is used.

import matplotlib.pyplot as plt
import numpy as np


x = np.array([0, 1, 2, 4])
y = np.array([5, 10])
z = np.array([100, 150, 200])
X, Y, Z = np.meshgrid(*[x, y, z], indexing="ij")
c = np.arange(0, X.size)

plt.figure()
for j, (a, e) in enumerate([(0, 0), (-60, 30)]):
    for i, vert_a in enumerate(["z", "y", "x"]):
        ax = plt.subplot(2, 3, j * 3 + 1 + i, projection="3d")
        pc = ax.scatter(X, Y, Z, c=c)
        ax.view_init(azim=a, elev=e, vertical_axis=vert_a)
        ax.set_title(f"azim={a}, elev={e}, vertical_axis='{vert_a}'")
        ax.set_xlabel("x")
        ax.set_ylabel("y")
        ax.set_zlabel("z")
        ax.set_box_aspect((1, 2, 3))

Before PR:
image

With PR:
image

PR checklist

@Illviljan Illviljan marked this pull request as ready for review April 14, 2024 13:42
@Illviljan
Copy link
Contributor Author

Illviljan commented Apr 14, 2024

Fixes set_aspect as well, since it uses set_box_aspect.

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as a3

plt.close('all')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(vertical_axis='y')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_xlabel('Z')
ax.axes.set_xlim3d(left=-1, right=3.5) 
ax.axes.set_ylim3d(bottom=0, top=10) 
ax.axes.set_zlim3d(bottom=-0.5, top=3.5) 
ax.set_aspect('equal')
    
def a(elemList):
    return np.array(elemList)

def drawTriangle(centre, size=1, color='y'):
    # A neuron is a triangle
    left = centre + a([- size / 2, - size / 2, 0])
    right = centre + a([size / 2, - size / 2, 0])
    top = centre + a([0, size / 2, 0])
    coords = a([left, top, right])
    tri = a3.art3d.Poly3DCollection([coords])
    tri.set_color(color)
    tri.set_edgecolor('k')
    ax.add_collection3d(tri)

for col in range(3):
    drawTriangle(a([col, 10, 0]))

With PR:
image

Copy link
Contributor

@scottshambaugh scottshambaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this as well! Two small notes, then I can approve the MR.

lib/mpl_toolkits/mplot3d/axes3d.py Outdated Show resolved Hide resolved

def get_proj(self):
"""Create the projection matrix from the current viewing position."""

# Transform to uniform world coordinates 0-1, 0-1, 0-1
box_aspect = self._roll_to_vertical(self._box_aspect)
# self._box_aspect = box_aspect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for removing this line? I'm not totally clear where it comes into play (when redrawing somewhere?), but note that the 3D test coverage isn't amazing so we should understand where behavior is changing. I would make sure that the plots are behaving as expected when you manually rotate the view.

Should remove rather than comment out if it's unneeded

Copy link
Contributor Author

@Illviljan Illviljan Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It never was in main. I was just testing different ideas here.

Conclusion here is that box_aspect has to be local, otherwise there will be a new vertical axis each click (or call to get_proj).

Co-authored-by: Scott Shambaugh <14363975+scottshambaugh@users.noreply.github.com>
Copy link
Contributor

@scottshambaugh scottshambaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failure looks unrelated to me, good to merge with a second reviewer!

@scottshambaugh
Copy link
Contributor

It looks like this needs a rebase to deconflict with your other MR that was just merged in.

@scottshambaugh scottshambaugh added this to the v3.9.1 milestone May 14, 2024
@scottshambaugh scottshambaugh changed the title Shift box_aspect according to vertical_axis [BUG]: Shift box_aspect according to vertical_axis May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants