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

Skip to content

Commit be3f489

Browse files
committed
TST/FIX twinx and twiny w/ constrainedlayout
1 parent af4fd77 commit be3f489

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4192,25 +4192,9 @@ def _make_twin_axes(self, *kl, **kwargs):
41924192
if 'sharex' in kwargs and 'sharey' in kwargs:
41934193
raise ValueError("Twinned Axes may share only one axis.")
41944194
ax2 = self.figure.add_axes(self.get_position(True), *kl, **kwargs)
4195-
41964195
self.set_adjustable('datalim')
41974196
ax2.set_adjustable('datalim')
41984197
self._twinned_axes.join(self, ax2)
4199-
# check if we have a layoutbox. If so, then set this axis
4200-
# gets the same poslayoutbox and layoutbox...
4201-
if self._layoutbox is not None:
4202-
name = self._layoutbox.name + 'twin' + layoutbox.seq_id()
4203-
ax2._layoutbox = layoutbox.LayoutBox(
4204-
parent=self._subplotspec._layoutbox,
4205-
name=name,
4206-
artist=ax2)
4207-
ax2._poslayoutbox = layoutbox.LayoutBox(
4208-
parent=ax2._layoutbox,
4209-
name=ax2._layoutbox.name+'.pos',
4210-
pos=True, subplot=True, artist=ax2)
4211-
# make the layout boxes be the same
4212-
ax2._layoutbox.constrain_same(self._layoutbox)
4213-
ax2._poslayoutbox.constrain_same(self._poslayoutbox)
42144198
return ax2
42154199

42164200
def twinx(self):
@@ -4263,6 +4247,7 @@ def twiny(self):
42634247
For those who are 'picking' artists while using twiny, pick
42644248
events are only called for the artists in the top-most axes.
42654249
"""
4250+
42664251
ax2 = self._make_twin_axes(sharey=self)
42674252
ax2.xaxis.tick_top()
42684253
ax2.xaxis.set_label_position('top')

lib/matplotlib/axes/_subplots.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ def _make_twin_axes(self, *kl, **kwargs):
183183
self.figure.add_subplot(ax2)
184184
self.set_adjustable('datalim')
185185
ax2.set_adjustable('datalim')
186+
187+
if self._layoutbox is not None and ax2._layoutbox is not None:
188+
# make the layout boxes be explicitly the same
189+
ax2._layoutbox.constrain_same(self._layoutbox)
190+
ax2._poslayoutbox.constrain_same(self._poslayoutbox)
191+
186192
self._twinned_axes.join(self, ax2)
187193
return ax2
188194

lib/matplotlib/gridspec.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,6 @@ def __init__(self, nrows, ncols, figure=None,
206206
height_ratios=height_ratios)
207207

208208
if (self.figure is None) or not self.figure.get_constrained_layout():
209-
_log.info("GridSpec must be called with the fig keyword if "
210-
"constrained_layout is used")
211209
self._layoutbox = None
212210
else:
213211
self.figure.init_layoutbox()

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,25 @@ def test_constrained_layout17():
355355
example_plot(ax2)
356356
example_plot(ax3)
357357
example_plot(ax4)
358+
359+
360+
def test_constrained_layout18():
361+
'Test twinx'
362+
fig, ax = plt.subplots(constrained_layout=True)
363+
ax2 = ax.twinx()
364+
example_plot(ax)
365+
example_plot(ax2, fontsize=24)
366+
fig.canvas.draw()
367+
assert all(ax.get_position().extents == ax2.get_position().extents)
368+
369+
370+
def test_constrained_layout19():
371+
'Test twiny'
372+
fig, ax = plt.subplots(constrained_layout=True)
373+
ax2 = ax.twiny()
374+
example_plot(ax)
375+
example_plot(ax2, fontsize=24)
376+
ax2.set_title('')
377+
ax.set_title('')
378+
fig.canvas.draw()
379+
assert all(ax.get_position().extents == ax2.get_position().extents)

0 commit comments

Comments
 (0)