Skip to content

Commit 66642ff

Browse files
authored
Merge pull request #27662 from meeseeksmachine/auto-backport-of-pr-27657-on-v3.8.x
Backport PR #27657 on branch v3.8.x (Fix Numpy 2.0 related test failures)
2 parents 1b8f49e + 72403c7 commit 66642ff

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

lib/matplotlib/pylab.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
bytes = __import__("builtins").bytes
6161
# We also don't want the numpy version of these functions
6262
abs = __import__("builtins").abs
63+
bool = __import__("builtins").bool
6364
max = __import__("builtins").max
6465
min = __import__("builtins").min
66+
pow = __import__("builtins").pow
6567
round = __import__("builtins").round

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5735,7 +5735,12 @@ def test_text_labelsize():
57355735
ax.tick_params(direction='out')
57365736

57375737

5738-
@image_comparison(['pie_default.png'])
5738+
# Note: The `pie` image tests were affected by Numpy 2.0 changing promotions
5739+
# (NEP 50). While the changes were only marginal, tolerances were introduced.
5740+
# These tolerances could likely go away when numpy 2.0 is the minimum supported
5741+
# numpy and the images are regenerated.
5742+
5743+
@image_comparison(['pie_default.png'], tol=0.01)
57395744
def test_pie_default():
57405745
# The slices will be ordered and plotted counter-clockwise.
57415746
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5748,7 +5753,7 @@ def test_pie_default():
57485753

57495754

57505755
@image_comparison(['pie_linewidth_0', 'pie_linewidth_0', 'pie_linewidth_0'],
5751-
extensions=['png'], style='mpl20')
5756+
extensions=['png'], style='mpl20', tol=0.01)
57525757
def test_pie_linewidth_0():
57535758
# The slices will be ordered and plotted counter-clockwise.
57545759
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5780,7 +5785,7 @@ def test_pie_linewidth_0():
57805785
plt.axis('equal')
57815786

57825787

5783-
@image_comparison(['pie_center_radius.png'], style='mpl20')
5788+
@image_comparison(['pie_center_radius.png'], style='mpl20', tol=0.005)
57845789
def test_pie_center_radius():
57855790
# The slices will be ordered and plotted counter-clockwise.
57865791
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5800,7 +5805,7 @@ def test_pie_center_radius():
58005805
plt.axis('equal')
58015806

58025807

5803-
@image_comparison(['pie_linewidth_2.png'], style='mpl20')
5808+
@image_comparison(['pie_linewidth_2.png'], style='mpl20', tol=0.01)
58045809
def test_pie_linewidth_2():
58055810
# The slices will be ordered and plotted counter-clockwise.
58065811
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5815,7 +5820,7 @@ def test_pie_linewidth_2():
58155820
plt.axis('equal')
58165821

58175822

5818-
@image_comparison(['pie_ccw_true.png'], style='mpl20')
5823+
@image_comparison(['pie_ccw_true.png'], style='mpl20', tol=0.01)
58195824
def test_pie_ccw_true():
58205825
# The slices will be ordered and plotted counter-clockwise.
58215826
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5830,7 +5835,7 @@ def test_pie_ccw_true():
58305835
plt.axis('equal')
58315836

58325837

5833-
@image_comparison(['pie_frame_grid.png'], style='mpl20')
5838+
@image_comparison(['pie_frame_grid.png'], style='mpl20', tol=0.002)
58345839
def test_pie_frame_grid():
58355840
# The slices will be ordered and plotted counter-clockwise.
58365841
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
@@ -5857,7 +5862,7 @@ def test_pie_frame_grid():
58575862
plt.axis('equal')
58585863

58595864

5860-
@image_comparison(['pie_rotatelabels_true.png'], style='mpl20')
5865+
@image_comparison(['pie_rotatelabels_true.png'], style='mpl20', tol=0.009)
58615866
def test_pie_rotatelabels_true():
58625867
# The slices will be ordered and plotted counter-clockwise.
58635868
labels = 'Hogwarts', 'Frogs', 'Dogs', 'Logs'
@@ -5872,7 +5877,7 @@ def test_pie_rotatelabels_true():
58725877
plt.axis('equal')
58735878

58745879

5875-
@image_comparison(['pie_no_label.png'])
5880+
@image_comparison(['pie_no_label.png'], tol=0.01)
58765881
def test_pie_nolabel_but_legend():
58775882
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
58785883
sizes = [15, 30, 45, 10]
@@ -5886,7 +5891,7 @@ def test_pie_nolabel_but_legend():
58865891
plt.legend()
58875892

58885893

5889-
@image_comparison(['pie_shadow.png'], style='mpl20')
5894+
@image_comparison(['pie_shadow.png'], style='mpl20', tol=0.002)
58905895
def test_pie_shadow():
58915896
# Also acts as a test for the shade argument of Shadow
58925897
sizes = [15, 30, 45, 10]

lib/matplotlib/tests/test_colors.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,38 +1371,38 @@ def test_scalarmappable_to_rgba(bytes):
13711371
# uint8 RGBA
13721372
x = np.ones((2, 3, 4), dtype=np.uint8)
13731373
expected = x.copy() if bytes else x.astype(np.float32)/255
1374-
np.testing.assert_array_equal(sm.to_rgba(x, bytes=bytes), expected)
1374+
np.testing.assert_almost_equal(sm.to_rgba(x, bytes=bytes), expected)
13751375
# uint8 RGB
13761376
expected[..., 3] = alpha_1
1377-
np.testing.assert_array_equal(sm.to_rgba(x[..., :3], bytes=bytes), expected)
1377+
np.testing.assert_almost_equal(sm.to_rgba(x[..., :3], bytes=bytes), expected)
13781378
# uint8 masked RGBA
13791379
xm = np.ma.masked_array(x, mask=np.zeros_like(x))
13801380
xm.mask[0, 0, 0] = True
13811381
expected = x.copy() if bytes else x.astype(np.float32)/255
13821382
expected[0, 0, 3] = 0
1383-
np.testing.assert_array_equal(sm.to_rgba(xm, bytes=bytes), expected)
1383+
np.testing.assert_almost_equal(sm.to_rgba(xm, bytes=bytes), expected)
13841384
# uint8 masked RGB
13851385
expected[..., 3] = alpha_1
13861386
expected[0, 0, 3] = 0
1387-
np.testing.assert_array_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)
1387+
np.testing.assert_almost_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)
13881388

13891389
# float RGBA
13901390
x = np.ones((2, 3, 4), dtype=float) * 0.5
13911391
expected = (x * 255).astype(np.uint8) if bytes else x.copy()
1392-
np.testing.assert_array_equal(sm.to_rgba(x, bytes=bytes), expected)
1392+
np.testing.assert_almost_equal(sm.to_rgba(x, bytes=bytes), expected)
13931393
# float RGB
13941394
expected[..., 3] = alpha_1
1395-
np.testing.assert_array_equal(sm.to_rgba(x[..., :3], bytes=bytes), expected)
1395+
np.testing.assert_almost_equal(sm.to_rgba(x[..., :3], bytes=bytes), expected)
13961396
# float masked RGBA
13971397
xm = np.ma.masked_array(x, mask=np.zeros_like(x))
13981398
xm.mask[0, 0, 0] = True
13991399
expected = (x * 255).astype(np.uint8) if bytes else x.copy()
14001400
expected[0, 0, 3] = 0
1401-
np.testing.assert_array_equal(sm.to_rgba(xm, bytes=bytes), expected)
1401+
np.testing.assert_almost_equal(sm.to_rgba(xm, bytes=bytes), expected)
14021402
# float masked RGB
14031403
expected[..., 3] = alpha_1
14041404
expected[0, 0, 3] = 0
1405-
np.testing.assert_array_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)
1405+
np.testing.assert_almost_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)
14061406

14071407

14081408
def test_failed_conversions():

0 commit comments

Comments
 (0)