Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efd40c1

Browse files
committed
tests for colorbar_multivar
1 parent 460047b commit efd40c1

9 files changed

Lines changed: 130 additions & 2 deletions

lib/matplotlib/_constrained_layout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ def reposition_colorbar(layoutgrids, cbax, renderer, *, offset=None, compress=Fa
759759
# Keep the pb x-coordinates but use actual y-coordinates
760760
pb = Bbox.from_extents(pb.x0, actual_pos_fig.y0,
761761
pb.x1, actual_pos_fig.y1)
762-
elif location in ('top', 'bottom'):
762+
else: # location in ('top', 'bottom'):
763763
# For horizontal colorbars, use the actual parent bbox width
764764
# for colorbar sizing
765765
# Keep the pb y-coordinates but use actual x-coordinates

lib/matplotlib/colorbar.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,6 +2226,8 @@ def make_multivar_axes(parents, n_variates, n_major, location=None, orientation=
22262226
n_major = n_variates
22272227
n_minor = 1
22282228
else:
2229+
if n_major == 0:
2230+
raise ValueError("n_major cannot be zero")
22292231
n_minor = n_variates // n_major
22302232
if n_major * n_minor < n_variates:
22312233
n_minor += 1
0 Bytes
Loading
17.2 KB
Loading
21.1 KB
Loading
21.5 KB
Loading
28.3 KB
Loading
12.4 KB
Loading

lib/matplotlib/tests/test_colorbar.py

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ def test_colorbar_format_string_and_old():
12431243
assert isinstance(cb._formatter, StrMethodFormatter)
12441244

12451245

1246-
@pytest.mark.parametrize('use_gridspec', [True])
1246+
@pytest.mark.parametrize('use_gridspec', [True, False])
12471247
@image_comparison(['bivar_cbar_locationing.png',
12481248
],
12491249
remove_text=True, savefig_kwarg={'dpi': 40}, tol=0.05)
@@ -1258,6 +1258,48 @@ def test_colorbar_bivar_location(use_gridspec):
12581258
fig.colorbar_bivar(mim, location=locations[i], use_gridspec=use_gridspec)
12591259

12601260

1261+
@image_comparison(['bivar_cbar_locationing_constrained.png',
1262+
],
1263+
remove_text=True, savefig_kwarg={'dpi': 40}, tol=0.05)
1264+
def test_colorbar_bivar_location_constrained():
1265+
data = (np.arange(12).reshape((3, 4)) % 4,
1266+
np.arange(12).reshape((3, 4)) % 3)
1267+
# -------------------
1268+
locations = ['left', 'right', 'top', 'bottom']
1269+
fig, axes = plt.subplots(2, 2, constrained_layout='constrained')
1270+
for i, ax in enumerate(axes.ravel()):
1271+
mim = ax.imshow(data, cmap='BiOrangeBlue')
1272+
fig.colorbar_bivar(mim, location=locations[i])
1273+
1274+
1275+
@image_comparison(['multivar_cbar_locationing.png',
1276+
],
1277+
remove_text=True, savefig_kwarg={'dpi': 40}, tol=0.05)
1278+
def test_colorbar_multivar_location():
1279+
data = (np.arange(12).reshape((3, 4)) % 4,
1280+
np.arange(12).reshape((3, 4)) % 3)
1281+
# -------------------
1282+
locations = ['left', 'right', 'top', 'bottom']
1283+
fig, axes = plt.subplots(2, 2)
1284+
for i, ax in enumerate(axes.ravel()):
1285+
mim = ax.imshow(data, cmap='2VarAddA')
1286+
fig.colorbar_multivar(mim, location=locations[i])
1287+
1288+
1289+
@image_comparison(['multivar_cbar_locationing_constrained.png',
1290+
],
1291+
remove_text=True, savefig_kwarg={'dpi': 40}, tol=0.05)
1292+
def test_colorbar_multivar_location_constrained():
1293+
data = (np.arange(12).reshape((3, 4)) % 4,
1294+
np.arange(12).reshape((3, 4)) % 3)
1295+
# -------------------
1296+
locations = ['left', 'right', 'top', 'bottom']
1297+
fig, axes = plt.subplots(2, 2, constrained_layout='constrained')
1298+
for i, ax in enumerate(axes.ravel()):
1299+
mim = ax.imshow(data, cmap='2VarAddA')
1300+
fig.colorbar_multivar(mim, location=locations[i])
1301+
1302+
12611303
@pytest.mark.parametrize('use_gridspec', [True, False])
12621304
@image_comparison(['bivar_cbar_sharing.png',
12631305
],
@@ -1282,6 +1324,27 @@ def test_colorbar_bivar_sharing(use_gridspec):
12821324
anchor=(0.8, 0.5), shrink=0.6, use_gridspec=use_gridspec)
12831325

12841326

1327+
@image_comparison(['multivar_cbar_sharing.png',
1328+
],
1329+
remove_text=True, savefig_kwarg={'dpi': 40}, tol=0.05)
1330+
def test_colorbar_multivar_sharing():
1331+
data = (np.arange(12).reshape((3, 4)) % 4,
1332+
np.arange(12).reshape((3, 4)) % 3,
1333+
np.arange(12).reshape((3, 4)) % 5)
1334+
# -------------------
1335+
plt.figure()
1336+
ax1 = plt.subplot(211, anchor='NE', aspect='equal')
1337+
plt.imshow(data, cmap='3VarAddA')
1338+
ax2 = plt.subplot(223)
1339+
plt.imshow(data, cmap='3VarAddA')
1340+
ax3 = plt.subplot(224)
1341+
plt.imshow(data, cmap='3VarAddA')
1342+
1343+
plt.colorbar_multivar(ax=[ax2, ax3, ax1], location='right')
1344+
plt.colorbar_multivar(ax=[ax2, ax3, ax1], location='left')
1345+
plt.colorbar_multivar(ax=[ax1], location='bottom')
1346+
1347+
12851348
@pytest.mark.parametrize('constrained', [False, True],
12861349
ids=['standard', 'constrained'])
12871350
def test_bivar_cbar_single_ax_panchor_east(constrained):
@@ -1462,6 +1525,22 @@ def test_bivar_cbar_remove_from_figure(nested_gridspecs, use_gridspec):
14621525
assert (pre_position.get_points() == post_position.get_points()).all()
14631526

14641527

1528+
def test_multivar_cbar_remove_from_figure():
1529+
"""Test `remove` with the specified ``use_gridspec`` setting."""
1530+
fig = plt.figure()
1531+
ax = fig.add_subplot()
1532+
mp = ax.pcolormesh([[[100, 1], [10, 1]], [[0.5, 0.2], [0.3, 1]]],
1533+
cmap='2VarAddA',
1534+
)
1535+
pre_position = ax.get_position()
1536+
cb = fig.colorbar_multivar(mp)
1537+
fig.subplots_adjust()
1538+
cb.remove()
1539+
fig.subplots_adjust()
1540+
post_position = ax.get_position()
1541+
assert (pre_position.get_points() == post_position.get_points()).all()
1542+
1543+
14651544
def test_bivar_cbar_remove_with_no_mappable():
14661545
fig, ax = plt.subplots()
14671546
norm = mpl.colors.MultiNorm(['linear', 'linear'])
@@ -1470,6 +1549,20 @@ def test_bivar_cbar_remove_with_no_mappable():
14701549
cb.remove()
14711550

14721551

1552+
def test_multivar_cbar_from_colorizer():
1553+
fig, ax = plt.subplots()
1554+
norm = mpl.colors.MultiNorm(['linear', 'linear'])
1555+
ca = mpl.colorizer.Colorizer('2VarAddA', norm)
1556+
cb = fig.colorbar_multivar(ca, ax=ax)
1557+
1558+
1559+
def test_multivar_cbar_from_colorizer_cax():
1560+
fig, axes = plt.subplots(1, 3)
1561+
norm = mpl.colors.MultiNorm(['linear', 'linear', 'linear'])
1562+
ca = mpl.colorizer.Colorizer('3VarAddA', norm)
1563+
cb = fig.colorbar_multivar(ca, caxes=axes)
1564+
1565+
14731566
def test_bivar_cbar_ticklocations():
14741567
norm = mpl.colors.MultiNorm(['linear', 'linear'])
14751568
ca = mpl.colorizer.Colorizer('BiOrangeBlue', norm)
@@ -1615,10 +1708,43 @@ def test_bivar_cbar_not_rasterized():
16151708
fig.colorbar_bivar(mp)
16161709

16171710

1711+
@image_comparison(['multivar_cbar_n_major.png',
1712+
],
1713+
remove_text=True, savefig_kwarg={'dpi': 40}, tol=0.05)
1714+
def test_colorbar_multivar_n_major():
1715+
data = (np.arange(12).reshape((3, 4)) % 4,
1716+
np.arange(12).reshape((3, 4)) % 3,
1717+
np.arange(12).reshape((3, 4)) % 5)
1718+
# -------------------
1719+
fig, axes = plt.subplots(4, 3, figsize=(8, 8), constrained_layout='constrained')
1720+
locations = ['left', 'right', 'top', 'bottom']
1721+
for i, axs in enumerate(axes):
1722+
for j, ax in enumerate(axs):
1723+
mim = ax.imshow(data, cmap='3VarAddA')
1724+
fig.colorbar_multivar(mim, n_major=j + 1, location=locations[i])
1725+
1726+
with pytest.raises(ValueError, match="cannot be zero"):
1727+
fig.colorbar_multivar(mim, n_major=0)
1728+
1729+
16181730
def test_cbar_wrong_figures():
16191731
fig0, ax0 = plt.subplots()
16201732
fig1, ax1 = plt.subplots()
16211733
im0 = ax0.imshow([[0, 1], [2, 3]])
16221734
im1 = ax1.imshow([[0, 1], [2, 3]])
16231735
with pytest.raises(ValueError, match="not all parents share"):
16241736
fig0.colorbar(im0, ax=[im0, im1])
1737+
1738+
1739+
def test_multivar_cbar_set_label_limits():
1740+
data = (np.arange(12).reshape((3, 4)) % 4,
1741+
np.arange(12).reshape((3, 4)) % 3,
1742+
np.arange(12).reshape((3, 4)) % 5)
1743+
# -------------------
1744+
fig, ax = plt.subplots(1, 1)
1745+
mim = ax.imshow(data, cmap='3VarAddA')
1746+
cbs = fig.colorbar_multivar(mim)
1747+
cbs[0].set_label('A')
1748+
assert len(cbs) == 3
1749+
mim.norm.vmin = (-1, -1, -1)
1750+
mim.norm.vmax = (1, 2, 3)

0 commit comments

Comments
 (0)