Skip to content

Commit 902922a

Browse files
committed
(1) turn off the grid after creating colorbar axes (2) added a test
1 parent 806f00a commit 902922a

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/matplotlib/figure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ def colorbar(
11431143
cax, kwargs = cbar.make_axes_gridspec(ax, **kwargs)
11441144
else:
11451145
cax, kwargs = cbar.make_axes(ax, **kwargs)
1146+
cax.grid(visible=False, which='both', axis='both')
11461147
else:
11471148
userax = True
11481149

lib/matplotlib/tests/test_colorbar.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,3 +938,15 @@ def test_boundaries():
938938
fig, ax = plt.subplots(figsize=(2, 2))
939939
pc = ax.pcolormesh(np.random.randn(10, 10), cmap='RdBu_r')
940940
cb = fig.colorbar(pc, ax=ax, boundaries=np.linspace(-3, 3, 7))
941+
942+
943+
def test_colorbar_no_warning_rcparams_grid_true():
944+
# github issue #21723 - If mpl style has 'axes.grid' = True,
945+
# fig.colorbar raises a warning about Auto-removal of grids
946+
# by pcolor() and pcolormesh(). This is fixed by PR #22216.
947+
plt.rcParams['axes.grid'] = True
948+
fig, ax = plt.subplots()
949+
ax.grid(False)
950+
im = ax.pcolormesh([0, 1], [0, 1], [[1]])
951+
# make sure that no warning is raised by fig.colorbar
952+
fig.colorbar(im)

0 commit comments

Comments
 (0)