Skip to content

Normalization of elevation and azimuth angles for surface plots #10762

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,16 +964,16 @@ def view_init(self, elev=None, azim=None):
"""

self.dist = 10

# normalize the angle to stay between [-180, 180]
if elev is None:
self.elev = self.initial_elev
self.elev = art3d.norm_angle(self.initial_elev)
else:
self.elev = elev
self.elev = art3d.norm_angle(elev)
Copy link
Contributor

Choose a reason for hiding this comment

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

If you're going to normalize it at all, elevation should be normalized to [-90, +90]

Copy link
Contributor Author

@jinshifen33 jinshifen33 Mar 19, 2018

Choose a reason for hiding this comment

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


if azim is None:
self.azim = self.initial_azim
self.azim = art3d.norm_angle(self.initial_azim)
else:
self.azim = azim
self.azim = art3d.norm_angle(azim)

def set_proj_type(self, proj_type):
"""
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 43 additions & 10 deletions lib/mpl_toolkits/tests/test_mplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,33 @@ def test_plot_3d_from_2d():
ax.plot(xs, ys, zs=0, zdir='y')


@image_comparison(baseline_images=['surface3d_rotate_gt_270deg'],
remove_text=True, extensions=['png'])
def test_rotate_gt_270deg():
elevation = 345
angle = None

fig = plt.figure()
ax = fig.gca(projection='3d')
# get test data, copied from axes3d.get_test_data(0.05)
# avoiding create too much dependency
x = y = np.arange(-3.0, 3.0, 0.05)
X, Y = np.meshgrid(x, y)

Z1 = np.exp(-(X**2 + Y**2) / 2) / (2 * np.pi)
Z2 = (np.exp(-(((X - 1) / 1.5)**2 + ((Y - 1) / 0.5)**2) / 2) /
(2 * np.pi * 0.5 * 1.5))
Z = Z2 - Z1

X = X * 10
Y = Y * 10
Z = Z * 500
# plot below
s = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, lw=0)
ax.view_init(azim=angle, elev=elevation)
ax.axis('off')


@image_comparison(baseline_images=['surface3d'], remove_text=True)
def test_surface3d():
fig = plt.figure()
Expand Down Expand Up @@ -316,10 +343,11 @@ def test_quiver3d():
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
np.sin(np.pi * z))
np.sin(np.pi * z))

ax.quiver(x, y, z, u, v, w, length=0.1, pivot='tip', normalize=True)


@image_comparison(baseline_images=['quiver3d_empty'], remove_text=True)
def test_quiver3d_empty():
fig = plt.figure()
Expand All @@ -330,10 +358,11 @@ def test_quiver3d_empty():
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
np.sin(np.pi * z))
np.sin(np.pi * z))

ax.quiver(x, y, z, u, v, w, length=0.1, pivot='tip', normalize=True)


@image_comparison(baseline_images=['quiver3d_masked'], remove_text=True)
def test_quiver3d_masked():
fig = plt.figure()
Expand All @@ -346,12 +375,13 @@ def test_quiver3d_masked():
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
np.sin(np.pi * z))
np.sin(np.pi * z))
u = np.ma.masked_where((-0.4 < x) & (x < 0.1), u, copy=False)
v = np.ma.masked_where((0.1 < y) & (y < 0.7), v, copy=False)

ax.quiver(x, y, z, u, v, w, length=0.1, pivot='tip', normalize=True)


@image_comparison(baseline_images=['quiver3d_pivot_middle'], remove_text=True,
extensions=['png'])
def test_quiver3d_pivot_middle():
Expand All @@ -363,10 +393,11 @@ def test_quiver3d_pivot_middle():
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
np.sin(np.pi * z))
np.sin(np.pi * z))

ax.quiver(x, y, z, u, v, w, length=0.1, pivot='middle', normalize=True)


@image_comparison(baseline_images=['quiver3d_pivot_tail'], remove_text=True,
extensions=['png'])
def test_quiver3d_pivot_tail():
Expand All @@ -378,7 +409,7 @@ def test_quiver3d_pivot_tail():
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
np.sin(np.pi * z))
np.sin(np.pi * z))

ax.quiver(x, y, z, u, v, w, length=0.1, pivot='tail', normalize=True)

Expand Down Expand Up @@ -425,17 +456,18 @@ def test_axes3d_labelpad():
def test_axes3d_cla():
# fixed in pull request 4553
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.set_axis_off()
ax.cla() # make sure the axis displayed is 3D (not 2D)


def test_plotsurface_1d_raises():
x = np.linspace(0.5, 10, num=100)
y = np.linspace(0.5, 10, num=100)
X, Y = np.meshgrid(x, y)
z = np.random.randn(100)

fig = plt.figure(figsize=(14,6))
fig = plt.figure(figsize=(14, 6))
ax = fig.add_subplot(1, 2, 1, projection='3d')
with pytest.raises(ValueError):
ax.plot_surface(X, Y, z)
Expand Down Expand Up @@ -534,6 +566,7 @@ def test_proj_axes_cube_ortho():
ax.set_xlim(-200, 200)
ax.set_ylim(-200, 200)


def test_rot():
V = [1, 0, 0, 1]
rotated_V = proj3d.rot_x(V, np.pi / 6)
Expand Down Expand Up @@ -674,9 +707,9 @@ def test_rgb_data(self):
x, y, z = np.indices((10, 10, 10))
voxels = (x == y) | (y == z)
colors = np.zeros((10, 10, 10, 3))
colors[...,0] = x/9.0
colors[...,1] = y/9.0
colors[...,2] = z/9.0
colors[..., 0] = x/9.0
colors[..., 1] = y/9.0
colors[..., 2] = z/9.0
ax.voxels(voxels, facecolors=colors)

@image_comparison(
Expand Down