8000 FIX: tickspacing for subfigures [skip circle] · matplotlib/matplotlib@ebe6d09 · GitHub
[go: up one dir, main page]

Skip to content

Commit ebe6d09

Browse files
committed
FIX: tickspacing for subfigures [skip circle]
1 parent 64e898a commit ebe6d09

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/matplotlib/axis.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,7 +2253,8 @@ def set_default_intervals(self):
22532253

22542254
def get_tick_space(self):
22552255
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
2256-
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72
2256+
ends = self.figure.dpi_scale_trans.inverted().transform(ends)
2257+
length = (ends[1][0] - ends[0][0]) * 72
22572258
# There is a heuristic here that the aspect ratio of tick text
22582259
# is no more than 3:1
22592260
size = self._get_tick_label_size('x') * 3
@@ -2513,7 +2514,8 @@ def set_default_intervals(self):
25132514

25142515
def get_tick_space(self):
25152516
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
2516-
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72
2517+
ends = self.figure.dpi_scale_trans.inverted().transform(ends)
2518+
length = (ends[1][1] - ends[0][1]) * 72
25172519
# Having a spacing of at least 2 just looks good.
25182520
size = self._get_tick_label_size('y') * 2
25192521
if size > 0:

lib/matplotlib/tests/test_figure.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,34 @@ def test_subfigure_spanning():
10311031
np.testing.assert_allclose(sub_figs[2].bbox.max, [w, h / 3])
10321032

10331033

1034+
@mpl.style.context('mpl20')
1035+
def test_subfigure_ticks():
1036+
# This tests a tick-spacing error that only seems applicable
1037+
# when the subfigures are saved to file. Its very hard to replicate
1038+
fig = plt.figure(constrained_layout=True, figsize=(10, 3))
1039+
# create left/right subfigs nested in bottom subfig
1040+
(subfig_bl, subfig_br) = fig.subfigures(1, 2, wspace=0.01,
1041+
width_ratios=[7, 2])
1042+
1043+
# put ax1-ax3 in gridspec of bottom-left subfig
1044+
gs = subfig_bl.add_gridspec(nrows=1, ncols=14)
1045+
1046+
ax1 = subfig_bl.add_subplot(gs[0, :1])
1047+
ax1.scatter(x=[-56.46881504821776, 24.179891162109396], y=[1500, 3600])
1048+
1049+
ax2 = subfig_bl.add_subplot(gs[0, 1:3], sharey=ax1)
1050+
ax2.scatter(x=[-126.5357270050049, 94.68456736755368], y=[1500, 3600])
1051+
ax3 = subfig_bl.add_subplot(gs[0, 3:14], sharey=ax1)
1052+
1053+
fig.set_dpi(120)
1054+
fig.draw_no_output()
1055+
ticks120 = ax2.get_xticks()
1056+
fig.set_dpi(300)
1057+
fig.draw_no_output()
1058+
ticks300 = ax2.get_xticks()
1059+
np.testing.assert_allclose(ticks120, ticks300)
1060+
1061+
10341062
def test_add_subplot_kwargs():
10351063
# fig.add_subplot() always creates new axes, even if axes kwargs differ.
10361064
fig = plt.figure()

0 commit comments

Comments
 (0)
0