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

Skip to content

Commit 5bf04bd

Browse files
tacaswellmeeseeksmachine
authored andcommitted
Backport PR #20771: FIX: tickspacing for subfigures
1 parent 433ec04 commit 5bf04bd

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

lib/matplotlib/axis.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,8 +2261,10 @@ def set_default_intervals(self):
22612261
self.stale = True
22622262

22632263
def get_tick_space(self):
2264-
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
2265-
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72
2264+
ends = mtransforms.Bbox.from_bounds(0, 0, 1, 1)
2265+
ends = ends.transformed(self.axes.transAxes -
2266+
self.figure.dpi_scale_trans)
2267+
length = ends.width * 72
22662268
# There is a heuristic here that the aspect ratio of tick text
22672269
# is no more than 3:1
22682270
size = self._get_tick_label_size('x') * 3
@@ -2526,8 +2528,10 @@ def set_default_intervals(self):
25262528
self.stale = True
25272529

25282530
def get_tick_space(self):
2529-
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
2530-
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72
2531+
ends = mtransforms.Bbox.from_bounds(0, 0, 1, 1)
2532+
ends = ends.transformed(self.axes.transAxes -
2533+
self.figure.dpi_scale_trans)
2534+
length = ends.height * 72
25312535
# Having a spacing of at least 2 just looks good.
25322536
size = self._get_tick_label_size('y') * 2
25332537
if size > 0:

lib/matplotlib/figure.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,6 @@ def __init__(self, parent, subplotspec, *,
19851985
self.subplotpars = parent.subplotpars
19861986
self.dpi_scale_trans = parent.dpi_scale_trans
19871987
self._axobservers = parent._axobservers
1988-
self.dpi = parent.dpi
19891988
self.canvas = parent.canvas
19901989
self.transFigure = parent.transFigure
19911990
self.bbox_relative = None
@@ -2006,6 +2005,14 @@ def __init__(self, parent, subplotspec, *,
20062005
if parent._layoutgrid is not None:
20072006
self.init_layoutgrid()
20082007

2008+
@property
2009+
def dpi(self):
2010+
return self._parent.dpi
2011+
2012+
@dpi.setter
2013+
def dpi(self, value):
2014+
self._parent.dpi = value
2015+
20092016
def _redo_transform_rel_fig(self, bbox=None):
20102017
"""
20112018
Make the transSubfigure bbox relative to Figure transform.

lib/matplotlib/tests/test_figure.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,51 @@ def test_subfigure_spanning():
10201020
np.testing.assert_allclose(sub_figs[2].bbox.max, [w, h / 3])
10211021

10221022

1023+
@mpl.style.context('mpl20')
1024+
def test_subfigure_ticks():
1025+
# This tests a tick-spacing error that only seems applicable
1026+
# when the subfigures are saved to file. It is very hard to replicate
1027+
fig = plt.figure(constrained_layout=True, figsize=(10, 3))
1028+
# create left/right subfigs nested in bottom subfig
1029+
(subfig_bl, subfig_br) = fig.subfigures(1, 2, wspace=0.01,
1030+
width_ratios=[7, 2])
1031+
1032+
# put ax1-ax3 in gridspec of bottom-left subfig
1033+
gs = subfig_bl.add_gridspec(nrows=1, ncols=14)
1034+
1035+
ax1 = subfig_bl.add_subplot(gs[0, :1])
1036+
ax1.scatter(x=[-56.46881504821776, 24.179891162109396], y=[1500, 3600])
1037+
1038+
ax2 = subfig_bl.add_subplot(gs[0, 1:3], sharey=ax1)
1039+
ax2.scatter(x=[-126.5357270050049, 94.68456736755368], y=[1500, 3600])
1040+
ax3 = subfig_bl.add_subplot(gs[0, 3:14], sharey=ax1)
1041+
1042+
fig.set_dpi(120)
1043+
fig.draw_no_output()
1044+
ticks120 = ax2.get_xticks()
1045+
fig.set_dpi(300)
1046+
fig.draw_no_output()
1047+
ticks300 = ax2.get_xticks()
1048+
np.testing.assert_allclose(ticks120, ticks300)
1049+
1050+
1051+
@image_comparison(['test_subfigure_scatter_size.png'], style='mpl20',
1052+
remove_text=True)
1053+
def test_subfigure_scatter_size():
1054+
# markers in the left- and right-most subplots should be the same
1055+
fig = plt.figure()
1056+
gs = fig.add_gridspec(1, 2)
1057+
ax0 = fig.add_subplot(gs[1])
1058+
ax0.scatter([1, 2, 3], [1, 2, 3], s=30, marker='s')
1059+
ax0.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s')
1060+
1061+
sfig = fig.add_subfigure(gs[0])
1062+
axs = sfig.subplots(1, 2)
1063+
for ax in [ax0, axs[0]]:
1064+
ax.scatter([1, 2, 3], [1, 2, 3], s=30, marker='s', color='r')
1065+
ax.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s', color='g')
1066+
1067+
10231068
def test_add_subplot_kwargs():
10241069
# fig.add_subplot() always creates new axes, even if axes kwargs differ.
10251070
fig = plt.figure()

0 commit comments

Comments
 (0)