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

Skip to content

Commit b77767f

Browse files
authored
Merge pull request #29013 from jklymak/fix-autofmtxdate
FIX: auto_fmtxdate for constrained layout
2 parents 9066b40 + 0cf4c2f commit b77767f

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/matplotlib/figure.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,15 @@ def autofmt_xdate(
194194
Selects which ticklabels to rotate.
195195
"""
196196
_api.check_in_list(['major', 'minor', 'both'], which=which)
197-
allsubplots = all(ax.get_subplotspec() for ax in self.axes)
198-
if len(self.axes) == 1:
197+
axes = [ax for ax in self.axes if ax._label != '<colorbar>']
198+
allsubplots = all(ax.get_subplotspec() for ax in axes)
199+
if len(axes) == 1:
199200
for label in self.axes[0].get_xticklabels(which=which):
200201
label.set_ha(ha)
201202
label.set_rotation(rotation)
202203
else:
203204
if allsubplots:
204-
for ax in self.get_axes():
205+
for ax in axes:
205206
if ax.get_subplotspec().is_last_row():
206207
for label in ax.get_xticklabels(which=which):
207208
label.set_ha(ha)
@@ -211,7 +212,8 @@ def autofmt_xdate(
211212
label.set_visible(False)
212213
ax.set_xlabel('')
213214

214-
if allsubplots:
215+
engine = self.get_layout_engine()
216+
if allsubplots and (engine is None or engine.adjust_compatible):
215217
self.subplots_adjust(bottom=bottom)
216218
self.stale = True
217219

lib/matplotlib/tests/test_figure.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,20 @@ def test_autofmt_xdate(which):
496496
assert int(label.get_rotation()) == angle
497497

498498

499+
def test_autofmt_xdate_colorbar_constrained():
500+
# check works with a colorbar.
501+
# with constrained layout, colorbars do not have a gridspec,
502+
# but autofmt_xdate checks if all axes have a gridspec before being
503+
# applied.
504+
fig, ax = plt.subplots(layout="constrained")
505+
im = ax.imshow([[1, 4, 6], [2, 3, 5]])
506+
plt.colorbar(im)
507+
fig.autofmt_xdate()
508+
fig.draw_without_rendering()
509+
label = ax.get_xticklabels(which='major')[1]
510+
assert label.get_rotation() == 30.0
511+
512+
499513
@mpl.style.context('default')
500514
def test_change_dpi():
501515
fig = plt.figure(figsize=(4, 4))

0 commit comments

Comments
 (0)