Skip to content

FIX: tickspacing for subfigures #20771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2252,8 +2252,10 @@ def set_default_intervals(self):
self.stale = True

def get_tick_space(self):
ends = self.axes.transAxes.transform([[0, 0], [1, 0]])
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72
ends = mtransforms.Bbox.from_bounds(0, 0, 1, 1)
ends = ends.transformed(self.axes.transAxes -
self.figure.dpi_scale_trans)
length = ends.width * 72
# There is a heuristic here that the aspect ratio of tick text
# is no more than 3:1
size = self._get_tick_label_size('x') * 3
Expand Down Expand Up @@ -2512,8 +2514,10 @@ def set_default_intervals(self):
self.stale = True

def get_tick_space(self):
ends = self.axes.transAxes.transform([[0, 0], [0, 1]])
length = ((ends[1][1] - ends[0][1]) / self.axes.figure.dpi) * 72
ends = mtransforms.Bbox.from_bounds(0, 0, 1, 1)
ends = ends.transformed(self.axes.transAxes -
self.figure.dpi_scale_trans)
length = ends.height * 72
# Having a spacing of at least 2 just looks good.
size = self._get_tick_label_size('y') * 2
if size > 0:
Expand Down
9 changes: 8 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,6 @@ def __init__(self, parent, subplotspec, *,
self.subplotpars = parent.subplotpars
self.dpi_scale_trans = parent.dpi_scale_trans
self._axobservers = parent._axobservers
self.dpi = parent.dpi
self.canvas = parent.canvas
self.transFigure = parent.transFigure
self.bbox_relative = None
Expand All @@ -2001,6 +2000,14 @@ def __init__(self, parent, subplotspec, *,
if parent._layoutgrid is not None:
self.init_layoutgrid()

@property
def dpi(self):
return self._parent.dpi

@dpi.setter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we want the setter here? The getter makes sense as we may need this in the transforms, but do we want to be able to change the dpi of the whole figure from a sub figure?

On the other hard it seems realtively harmless and makes sure that subfigures feel as like figures as they can.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tried it, and it breaks some tests without the setter. I did not investigate any further though, so I don't know if that's a real problem or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to mess around with that. I'd say dpi is one of our more fragile concepts.

def dpi(self, value):
self._parent.dpi = value

def _redo_transform_rel_fig(self, bbox=None):
"""
Make the transSubfigure bbox relative to Figure transform.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,51 @@ def test_subfigure_spanning():
np.testing.assert_allclose(sub_figs[2].bbox.max, [w, h / 3])


@mpl.style.context('mpl20')
def test_subfigure_ticks():
# This tests a tick-spacing error that only seems applicable
# when the subfigures are saved to file. It is very hard to replicate
fig = plt.figure(constrained_layout=True, figsize=(10, 3))
# create left/right subfigs nested in bottom subfig
(subfig_bl, subfig_br) = fig.subfigures(1, 2, wspace=0.01,
width_ratios=[7, 2])

# put ax1-ax3 in gridspec of bottom-left subfig
gs = subfig_bl.add_gridspec(nrows=1, ncols=14)

ax1 = subfig_bl.add_subplot(gs[0, :1])
ax1.scatter(x=[-56.46881504821776, 24.179891162109396], y=[1500, 3600])

ax2 = subfig_bl.add_subplot(gs[0, 1:3], sharey=ax1)
ax2.scatter(x=[-126.5357270050049, 94.68456736755368], y=[1500, 3600])
ax3 = subfig_bl.add_subplot(gs[0, 3:14], sharey=ax1)

fig.set_dpi(120)
fig.draw_no_output()
ticks120 = ax2.get_xticks()
fig.set_dpi(300)
fig.draw_no_output()
ticks300 = ax2.get_xticks()
np.testing.assert_allclose(ticks120, ticks300)


@image_comparison(['test_subfigure_scatter_size.png'], style='mpl20',
remove_text=True)
def test_subfigure_scatter_size():
# markers in the left- and right-most subplots should be the same
fig = plt.figure()
gs = fig.add_gridspec(1, 2)
ax0 = fig.add_subplot(gs[1])
ax0.scatter([1, 2, 3], [1, 2, 3], s=30, marker='s')
ax0.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s')

sfig = fig.add_subfigure(gs[0])
axs = sfig.subplots(1, 2)
for ax in [ax0, axs[0]]:
ax.scatter([1, 2, 3], [1, 2, 3], s=30, marker='s', color='r')
ax.scatter([3, 4, 5], [1, 2, 3], s=[20, 30, 40], marker='s', color='g')


def test_add_subplot_kwargs():
# fig.add_subplot() always creates new axes, even if axes kwargs differ.
fig = plt.figure()
Expand Down