8000 FIX: make subfigure dpi a setter/getter property · matplotlib/matplotlib@3e7dc9b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e7dc9b

Browse files
committed
FIX: make subfigure dpi a setter/getter property
1 parent 357cfe5 commit 3e7dc9b

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

lib/matplotlib/axis.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,9 +2252,10 @@ def set_default_intervals(self):
22522252
self.stale = True
22532253

22542254
def get_tick_space(self):
2255-
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
2256-
ends = self.figure.dpi_scale_trans.inverted().transform(ends)
2257-
length = (ends[1][0] - ends[0][0]) * 72
2255+
ends = mtransforms.Bbox.from_bounds(0, 0, 1, 1)
2256+
ends = ends.transformed(self.axes.transAxes -
2257+
self.figure.dpi_scale_trans)
2258+
length = ends.width * 72
22582259
# There is a heuristic here that the aspect ratio of tick text
22592260
# is no more than 3:1
22602261
size = self._get_tick_label_size('x') * 3
@@ -2513,9 +2514,10 @@ def set_default_intervals(self):
25132514
self.stale = True
25142515

25152516
def get_tick_space(self):
2516-
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
2517-
ends = self.figure.dpi_scale_trans.inverted().transform(ends)
2518-
length = (ends[1][1] - ends[0][1]) * 72
2517+
ends = mtransforms.Bbox.from_bounds(0, 0, 1, 1)
2518+
ends = ends.transformed(self.axes.transAxes -
2519+
self.figure.dpi_scale_trans)
2520+
length = ends.height * 72
25192521
# Having a spacing of at least 2 just looks good.
25202522
size = self._get_tick_label_size('y') * 2
25212523
if size > 0:

lib/matplotlib/figure.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,6 @@ def __init__(self, parent, subplotspec, *,
19801980
self.subplotpars = parent.subplotpars
19811981
self.dpi_scale_trans = parent.dpi_scale_trans
19821982
self._axobservers = parent._axobservers
1983-
self.dpi = parent.dpi
19841983
self.canvas = parent.canvas
19851984
self.transFigure = parent.transFigure
19861985
self.bbox_relative = None
@@ -2001,6 +2000,14 @@ def __init__(self, parent, subplotspec, *,
20012000
if parent._layoutgrid is not None:
20022001
self.init_layoutgrid()
20032002

2003+
@property
2004+
def dpi(self):
2005+
return self._parent.dpi
2006+
2007+
@dpi.setter
2008+
def dpi(self, value):
2009+
self._parent.dpi = value
2010+
20042011
def _redo_transform_rel_fig(self, bbox=None):
20052012
"""
20062013
Make the transSubfigure bbox relative to Figure transform.
Loading

lib/matplotlib/tests/test_figure.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,23 @@ def test_subfigure_ticks():
10591059
np.testing.assert_allclose(ticks120, ticks300)
10601060

10611061

1062+
@image_comparison(['test_subfigure_scatter_size.png'], style='mpl20',
1063+
remove_text=True)
1064+
def test_subfigure_scatter_size():
1065+
# markers in the left- and right-most subplots should be the same
1066+
fig = plt.figure()
1067+
gs = fig.add_gridspec(1, 2)
1068+
ax0 = fig.add_subplot(gs[1])
1069+
ax0.scatter([1, 2, 3], [1, 2, 3], s=30, marker='s')
1070+
ax0.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s')
1071+
1072+
sfig = fig.add_subfigure(gs[0])
1073+
axs = sfig.subplots(1, 2)
1074+
for ax in [ax0, axs[0]]:
1075+
ax.scatter([1, 2, 3], [1, 2, 3], s=30, marker='s', color='r')
1076+
ax.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s', color='g')
1077+
1078+
10621079
def test_add_subplot_kwargs():
10631080
# fig.add_subplot() always creates new axes, even if axes kwargs differ.
10641081
fig = plt.figure()

0 commit comments

Comments
 (0)
0