Skip to content

Commit abcd97a

Browse files
committed
Add tests for mplot3d
1 parent 51ce677 commit abcd97a

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed
62.7 KB
Loading

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from matplotlib.testing.decorators import image_comparison, check_figures_equal
1212
from matplotlib.testing.widgets import mock_event
1313
from matplotlib.collections import LineCollection, PolyCollection
14-
from matplotlib.patches import Circle
14+
from matplotlib.patches import Circle, PathPatch
15+
from matplotlib.path import Path
16+
from matplotlib.text import Text
1517

1618
import matplotlib.pyplot as plt
1719
import numpy as np
@@ -131,6 +133,17 @@ def test_contour3d():
131133
ax.set_zlim(-100, 100)
132134

133135

136+
@mpl3d_image_comparison(['contour3d_extend3d.png'])
137+
def test_contour3d_extend3d():
138+
fig = plt.figure()
139+
ax = fig.add_subplot(projection='3d')
140+
X, Y, Z = axes3d.get_test_data(0.05)
141+
ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm, extend3d=True)
142+
ax.set_xlim(-30, 30)
143+
ax.set_ylim(-20, 40)
144+
ax.set_zlim(-80, 80)
145+
146+
134147
@mpl3d_image_comparison(['contourf3d.png'])
135148
def test_contourf3d():
136149
fig = plt.figure()
@@ -1029,6 +1042,9 @@ def test_autoscale():
10291042
ax.set_autoscalez_on(True)
10301043
ax.plot([0, 2], [0, 2], [0, 2])
10311044
assert ax.get_w_lims() == (0, 1, -.1, 1.1, -.4, 2.4)
1045+
ax.autoscale(axis='x')
1046+
ax.plot([0, 2], [0, 2], [0, 2])
1047+
assert ax.get_w_lims() == (0, 2, -.1, 1.1, -.4, 2.4)
10321048

10331049

10341050
@pytest.mark.parametrize('axis', ('x', 'y', 'z'))
@@ -1643,6 +1659,83 @@ def test_computed_zorder():
16431659
ax.axis('off')
16441660

16451661

1662+
def test_format_coord():
1663+
fig = plt.figure()
1664+
ax = fig.add_subplot(projection='3d')
1665+
x = np.arange(10)
1666+
ax.plot(x, np.sin(x))
1667+
fig.canvas.draw()
1668+
assert ax.format_coord(0, 0) == 'x=1.8066, y=1.0367, z=−0.0553'
1669+
# Modify parameters
1670+
ax.view_init(roll=30, vertical_axis="y")
1671+
fig.canvas.draw()
1672+
assert ax.format_coord(0, 0) == 'x=9.1651, y=−0.9215, z=−0.0359'
1673+
# Reset parameters
1674+
ax.view_init()
1675+
fig.canvas.draw()
1676+
assert ax.format_coord(0, 0) == 'x=1.8066, y=1.0367, z=−0.0553'
1677+
1678+
1679+
def test_get_axis_position():
1680+
fig = plt.figure()
1681+
ax = fig.add_subplot(projection='3d')
1682+
x = np.arange(10)
1683+
ax.plot(x, np.sin(x))
1684+
fig.canvas.draw()
1685+
assert ax.get_axis_position() == (False, True, False)
1686+
1687+
1688+
def test_margins():
1689+
fig = plt.figure()
1690+
ax = fig.add_subplot(projection='3d')
1691+
ax.margins(0.2)
1692+
assert ax.margins() == (0.2, 0.2, 0.2)
1693+
ax.margins(0.1, 0.2, 0.3)
1694+
assert ax.margins() == (0.1, 0.2, 0.3)
1695+
ax.margins(x=0)
1696+
assert ax.margins() == (0, 0.2, 0.3)
1697+
ax.margins(y=0.1)
1698+
assert ax.margins() == (0, 0.1, 0.3)
1699+
ax.margins(z=0)
1700+
assert ax.margins() == (0, 0.1, 0)
1701+
1702+
1703+
def test_margins_errors():
1704+
fig = plt.figure()
1705+
ax = fig.add_subplot(projection='3d')
1706+
with pytest.raises(TypeError, match="Cannot pass"):
1707+
ax.margins(0.2, x=0.4, y=0.4, z=0.4)
1708+
with pytest.raises(TypeError, match="Must pass"):
1709+
ax.margins(0.2, 0.4)
1710+
1711+
1712+
@check_figures_equal(extensions=["png"])
1713+
def test_text_3d(fig_test, fig_ref):
1714+
ax = fig_ref.add_subplot(projection="3d")
1715+
txt = Text(0.5, 0.5, r'Foo bar $\int$')
1716+
art3d.text_2d_to_3d(txt, z=1)
1717+
ax.add_artist(txt)
1718+
assert txt.get_position_3d() == (0.5, 0.5, 1)
1719+
1720+
ax = fig_test.add_subplot(projection="3d")
1721+
t3d = art3d.Text3D(0.5, 0.5, 1, r'Foo bar $\int$')
1722+
ax.add_artist(t3d)
1723+
assert t3d.get_position_3d() == (0.5, 0.5, 1)
1724+
1725+
1726+
@check_figures_equal(extensions=["png"])
1727+
def test_pathpatch_3d(fig_test, fig_ref):
1728+
ax = fig_ref.add_subplot(projection="3d")
1729+
path = Path.unit_rectangle()
1730+
patch = PathPatch(path)
1731+
art3d.pathpatch_2d_to_3d(patch, z=(0, 0.5, 0.7, 1, 0), zdir='y')
1732+
ax.add_artist(patch)
1733+
1734+
ax = fig_test.add_subplot(projection="3d")
1735+
pp3d = art3d.PathPatch3D(path, zs=(0, 0.5, 0.7, 1, 0), zdir='y')
1736+
ax.add_artist(pp3d)
1737+
1738+
16461739
@image_comparison(baseline_images=['scatter_spiral.png'],
16471740
remove_text=True,
16481741
style='default')

0 commit comments

Comments
 (0)