🌐 AI搜索 & 代理 主页
Skip to content

Commit b3d74fc

Browse files
committed
Increase precision and overhaul tests
1 parent 9b4ec6d commit b3d74fc

File tree

3 files changed

+68
-60
lines changed

3 files changed

+68
-60
lines changed

lib/matplotlib/_cm_bivar.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,30 +1276,30 @@
12761276
]).reshape((65, 65, 3))
12771277

12781278
BiOrangeBlue = np.array(
1279-
[0.000, 0.000, 0.000, 0.000, 0.062, 0.125, 0.000, 0.125, 0.250, 0.000,
1280-
0.188, 0.375, 0.000, 0.250, 0.500, 0.000, 0.312, 0.625, 0.000, 0.375,
1281-
0.750, 0.000, 0.438, 0.875, 0.000, 0.500, 1.000, 0.125, 0.062, 0.000,
1282-
0.125, 0.125, 0.125, 0.125, 0.188, 0.250, 0.125, 0.250, 0.375, 0.125,
1283-
0.312, 0.500, 0.125, 0.375, 0.625, 0.125, 0.438, 0.750, 0.125, 0.500,
1284-
0.875, 0.125, 0.562, 1.000, 0.250, 0.125, 0.000, 0.250, 0.188, 0.125,
1285-
0.250, 0.250, 0.250, 0.250, 0.312, 0.375, 0.250, 0.375, 0.500, 0.250,
1286-
0.438, 0.625, 0.250, 0.500, 0.750, 0.250, 0.562, 0.875, 0.250, 0.625,
1287-
1.000, 0.375, 0.188, 0.000, 0.375, 0.250, 0.125, 0.375, 0.312, 0.250,
1288-
0.375, 0.375, 0.375, 0.375, 0.438, 0.500, 0.375, 0.500, 0.625, 0.375,
1289-
0.562, 0.750, 0.375, 0.625, 0.875, 0.375, 0.688, 1.000, 0.500, 0.250,
1290-
0.000, 0.500, 0.312, 0.125, 0.500, 0.375, 0.250, 0.500, 0.438, 0.375,
1291-
0.500, 0.500, 0.500, 0.500, 0.562, 0.625, 0.500, 0.625, 0.750, 0.500,
1292-
0.688, 0.875, 0.500, 0.750, 1.000, 0.625, 0.312, 0.000, 0.625, 0.375,
1293-
0.125, 0.625, 0.438, 0.250, 0.625, 0.500, 0.375, 0.625, 0.562, 0.500,
1294-
0.625, 0.625, 0.625, 0.625, 0.688, 0.750, 0.625, 0.750, 0.875, 0.625,
1295-
0.812, 1.000, 0.750, 0.375, 0.000, 0.750, 0.438, 0.125, 0.750, 0.500,
1296-
0.250, 0.750, 0.562, 0.375, 0.750, 0.625, 0.500, 0.750, 0.688, 0.625,
1297-
0.750, 0.750, 0.750, 0.750, 0.812, 0.875, 0.750, 0.875, 1.000, 0.875,
1298-
0.438, 0.000, 0.875, 0.500, 0.125, 0.875, 0.562, 0.250, 0.875, 0.625,
1299-
0.375, 0.875, 0.688, 0.500, 0.875, 0.750, 0.625, 0.875, 0.812, 0.750,
1300-
0.875, 0.875, 0.875, 0.875, 0.938, 1.000, 1.000, 0.500, 0.000, 1.000,
1301-
0.562, 0.125, 1.000, 0.625, 0.250, 1.000, 0.688, 0.375, 1.000, 0.750,
1302-
0.500, 1.000, 0.812, 0.625, 1.000, 0.875, 0.750, 1.000, 0.938, 0.875,
1279+
[0.000, 0.000, 0.000, 0.000, 0.0625, 0.125, 0.000, 0.125, 0.250, 0.000,
1280+
0.1875, 0.375, 0.000, 0.250, 0.500, 0.000, 0.3125, 0.625, 0.000, 0.375,
1281+
0.750, 0.000, 0.4375, 0.875, 0.000, 0.500, 1.000, 0.125, 0.0625, 0.000,
1282+
0.125, 0.125, 0.125, 0.125, 0.1875, 0.250, 0.125, 0.250, 0.375, 0.125,
1283+
0.3125, 0.500, 0.125, 0.375, 0.625, 0.125, 0.4375, 0.750, 0.125, 0.500,
1284+
0.875, 0.125, 0.5625, 1.000, 0.250, 0.125, 0.000, 0.250, 0.1875, 0.125,
1285+
0.250, 0.250, 0.250, 0.250, 0.3125, 0.375, 0.250, 0.375, 0.500, 0.250,
1286+
0.4375, 0.625, 0.250, 0.500, 0.750, 0.250, 0.5625, 0.875, 0.250, 0.625,
1287+
1.000, 0.375, 0.1875, 0.000, 0.375, 0.250, 0.125, 0.375, 0.3125, 0.250,
1288+
0.375, 0.375, 0.375, 0.375, 0.4375, 0.500, 0.375, 0.500, 0.625, 0.375,
1289+
0.5625, 0.750, 0.375, 0.625, 0.875, 0.375, 0.6875, 1.000, 0.500, 0.250,
1290+
0.000, 0.500, 0.3125, 0.125, 0.500, 0.375, 0.250, 0.500, 0.4375, 0.375,
1291+
0.500, 0.500, 0.500, 0.500, 0.5625, 0.625, 0.500, 0.625, 0.750, 0.500,
1292+
0.6875, 0.875, 0.500, 0.750, 1.000, 0.625, 0.3125, 0.000, 0.625, 0.375,
1293+
0.125, 0.625, 0.4375, 0.250, 0.625, 0.500, 0.375, 0.625, 0.5625, 0.500,
1294+
0.625, 0.625, 0.625, 0.625, 0.6875, 0.750, 0.625, 0.750, 0.875, 0.625,
1295+
0.8125, 1.000, 0.750, 0.375, 0.000, 0.750, 0.4375, 0.125, 0.750, 0.500,
1296+
0.250, 0.750, 0.5625, 0.375, 0.750, 0.625, 0.500, 0.750, 0.6875, 0.625,
1297+
0.750, 0.750, 0.750, 0.750, 0.8125, 0.875, 0.750, 0.875, 1.000, 0.875,
1298+
0.4375, 0.000, 0.875, 0.500, 0.125, 0.875, 0.5625, 0.250, 0.875, 0.625,
1299+
0.375, 0.875, 0.6875, 0.500, 0.875, 0.750, 0.625, 0.875, 0.8125, 0.750,
1300+
0.875, 0.875, 0.875, 0.875, 0.9375, 1.000, 1.000, 0.500, 0.000, 1.000,
1301+
0.5625, 0.125, 1.000, 0.625, 0.250, 1.000, 0.6875, 0.375, 1.000, 0.750,
1302+
0.500, 1.000, 0.8125, 0.625, 1.000, 0.875, 0.750, 1.000, 0.9375, 0.875,
13031303
1.000, 1.000, 1.000,
13041304
]).reshape((9, 9, 3))
13051305

lib/matplotlib/tests/test_colors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,13 +2127,13 @@ def test_colorizer_multinorm_implicit():
21272127

21282128
# test call with two single values
21292129
data = [0.1, 0.2]
2130-
res = (0.098039, 0.149212, 0.2, 1.0)
2130+
res = (0.098039, 0.149020, 0.2, 1.0)
21312131
assert_array_almost_equal(ca.to_rgba(data), res)
21322132

21332133
# test call with two 1d arrays
21342134
data = [[0.1, 0.2], [0.3, 0.4]]
2135-
res = [[0.09803922, 0.19832211, 0.29803922, 1.],
2136-
[0.2, 0.29972, 0.4, 1.]]
2135+
res = [[0.09803922, 0.19803922, 0.29803922, 1.],
2136+
[0.2, 0.3, 0.4, 1.]]
21372137
assert_array_almost_equal(ca.to_rgba(data), res)
21382138

21392139
# test call with two 2d arrays
@@ -2191,7 +2191,7 @@ def test_colorizer_multinorm_explicit():
21912191

21922192
# test call with two single values
21932193
data = [0.1, 0.2]
2194-
res = (0.098039, 0.374506, 0.65098, 1.)
2194+
res = (0.098039, 0.374510, 0.65098, 1.)
21952195
assert_array_almost_equal(ca.to_rgba(data), res)
21962196

21972197

lib/matplotlib/tests/test_multivariate_colormaps.py

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,18 @@ def test_bivar_cmap_call_tuple():
220220

221221
def test_bivar_cmap_lut_smooth():
222222
cmap = mpl.bivar_colormaps['BiOrangeBlue']
223+
223224
assert_allclose(cmap.lut[:, 0, 0], np.linspace(0, 1, 256))
224-
assert_allclose(cmap.lut[:, 0, 1], np.linspace(0, 0.5, 256), atol=1e-3)
225-
assert_allclose(cmap.lut[0, :, 1], np.linspace(0, 0.5, 256), atol=1e-3)
225+
assert_allclose(cmap.lut[:, 255, 0], np.linspace(0, 1, 256))
226+
assert_allclose(cmap.lut[:, 0, 1], np.linspace(0, 0.5, 256))
227+
assert_allclose(cmap.lut[:, 153, 1], np.linspace(0.3, 0.8, 256))
228+
assert_allclose(cmap.lut[:, 255, 1], np.linspace(0.5, 1, 256))
229+
230+
assert_allclose(cmap.lut[0, :, 1], np.linspace(0, 0.5, 256))
231+
assert_allclose(cmap.lut[102, :, 1], np.linspace(0.2, 0.7, 256))
232+
assert_allclose(cmap.lut[255, :, 1], np.linspace(0.5, 1, 256))
226233
assert_allclose(cmap.lut[0, :, 2], np.linspace(0, 1, 256))
234+
assert_allclose(cmap.lut[255, :, 2], np.linspace(0, 1, 256))
227235

228236

229237
def test_bivar_cmap_call():
@@ -326,23 +334,27 @@ def test_bivar_cmap_1d_origin():
326334
"""
327335
Test getting 1D colormaps with different origins
328336
"""
329-
cmap = mpl.bivar_colormaps['BiOrangeBlue']
330-
assert_allclose(cmap[0](1.), (1., 0.5, 0., 1.))
331-
assert_allclose(cmap[1](1.), (0., 0.5, 1., 1.))
332-
333-
cmap = mpl.bivar_colormaps['BiOrangeBlue'].with_extremes(origin=(0, 1))
334-
assert_allclose(cmap[0](1.), (1., 1., 1., 1.))
335-
assert_allclose(cmap[1](1.), (0., 0.5, 1., 1.))
336-
337-
cmap = mpl.bivar_colormaps['BiOrangeBlue'].with_extremes(origin=(0.5, 0.5))
338-
assert_allclose(cmap[0](0.5),
339-
(0.5019607843137255, 0.5019453440984237, 0.5019607843137255, 1))
340-
assert_allclose(cmap[1](0.5),
341-
(0.5019607843137255, 0.5019453440984237, 0.5019607843137255, 1))
342-
343-
cmap = mpl.bivar_colormaps['BiOrangeBlue'].with_extremes(origin=(1, 1))
344-
assert_allclose(cmap[0](1.), (1., 1., 1., 1.))
345-
assert_allclose(cmap[1](1.), (1., 1., 1., 1.))
337+
cmap0 = mpl.bivar_colormaps['BiOrangeBlue']
338+
assert_allclose(cmap0[0].colors[:, 0], np.linspace(0, 1, 256))
339+
assert_allclose(cmap0[0].colors[:, 1], np.linspace(0, 0.5, 256))
340+
assert_allclose(cmap0[0].colors[:, 2], 0)
341+
assert_allclose(cmap0[1].colors[:, 0], 0)
342+
assert_allclose(cmap0[1].colors[:, 1], np.linspace(0, 0.5, 256))
343+
assert_allclose(cmap0[1].colors[:, 2], np.linspace(0, 1, 256))
344+
345+
cmap1 = cmap0.with_extremes(origin=(0, 1))
346+
assert_allclose(cmap1[0].colors[:, 0], np.linspace(0, 1, 256))
347+
assert_allclose(cmap1[0].colors[:, 1], np.linspace(0.5, 1, 256))
348+
assert_allclose(cmap1[0].colors[:, 2], 1)
349+
assert_allclose(cmap1[1].colors, cmap0[1].colors)
350+
351+
cmap2 = cmap0.with_extremes(origin=(0.2, 0.4))
352+
assert_allclose(cmap2[0].colors[:, 0], np.linspace(0, 1, 256))
353+
assert_allclose(cmap2[0].colors[:, 1], np.linspace(0.2, 0.7, 256))
354+
assert_allclose(cmap2[0].colors[:, 2], 0.4)
355+
assert_allclose(cmap2[1].colors[:, 0], 0.2)
356+
assert_allclose(cmap2[1].colors[:, 1], np.linspace(0.1, 0.6, 256))
357+
assert_allclose(cmap2[1].colors[:, 2], np.linspace(0, 1, 256))
346358

347359
with pytest.raises(KeyError,
348360
match="only 0 or 1 are valid keys"):
@@ -454,22 +466,18 @@ def test_bivar_cmap_from_image():
454466

455467

456468
def test_bivar_resample():
457-
cmap = mpl.bivar_colormaps['BiOrangeBlue'].resampled((2, 2))
458-
assert_allclose(cmap((0.25, 0.25)), (0, 0, 0, 1))
459-
460-
cmap = mpl.bivar_colormaps['BiOrangeBlue'].resampled((-2, 2))
461-
assert_allclose(cmap((0.25, 0.25)), (1., 0.5, 0., 1.))
469+
cmap = mpl.bivar_colormaps['BiOrangeBlue']
462470

463-
cmap = mpl.bivar_colormaps['BiOrangeBlue'].resampled((2, -2))
464-
assert_allclose(cmap((0.25, 0.25)), (0., 0.5, 1., 1.))
471+
assert_allclose(cmap.resampled((2, 2))((0.25, 0.25)), (0, 0, 0, 1))
472+
assert_allclose(cmap.resampled((-2, 2))((0.25, 0.25)), (1., 0.5, 0., 1.))
473+
assert_allclose(cmap.resampled((2, -2))((0.25, 0.25)), (0., 0.5, 1., 1.))
474+
assert_allclose(cmap.resampled((-2, -2))((0.25, 0.25)), (1, 1, 1, 1))
465475

466-
cmap = mpl.bivar_colormaps['BiOrangeBlue'].resampled((-2, -2))
467-
assert_allclose(cmap((0.25, 0.25)), (1, 1, 1, 1))
476+
assert_allclose(cmap((0.8, 0.4)), (0.8, 0.6, 0.4, 1.))
477+
assert_allclose(cmap.reversed()((1 - 0.8, 1 - 0.4)), (0.8, 0.6, 0.4, 1.))
468478

469-
cmap = mpl.bivar_colormaps['BiOrangeBlue'].reversed()
470-
assert_allclose(cmap((0.25, 0.25)), (0.74902, 0.74902, 0.74902, 1.), atol=1e-5)
471-
cmap = mpl.bivar_colormaps['BiOrangeBlue'].transposed()
472-
assert_allclose(cmap((0.25, 0.25)), (0.25098, 0.25098, 0.25098, 1.), atol=1e-5)
479+
assert_allclose(cmap((0.6, 0.2)), (0.6, 0.4, 0.2, 1.))
480+
assert_allclose(cmap.transposed()((0.2, 0.6)), (0.6, 0.4, 0.2, 1.))
473481

474482
with pytest.raises(ValueError, match="lutshape must be of length"):
475483
cmap = cmap.resampled(4)

0 commit comments

Comments
 (0)