diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index fa0a42f17c99..4c563e19d905 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -567,7 +567,11 @@ def get_window_extent(self, *args, **kwargs): get the axes bounding box in display space; *args* and *kwargs* are empty """ - return self.bbox + bbox = self.bbox + x_pad = self.xaxis.get_tick_padding() + y_pad = self.yaxis.get_tick_padding() + return mtransforms.Bbox([[bbox.x0 - x_pad, bbox.y0 - y_pad], + [bbox.x1 + x_pad, bbox.y1 + y_pad]]) def _init_axis(self): "move this out of __init__ because non-separable axes don't use it" diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 6597d70f8ac7..d5cca9d6ea63 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -169,6 +169,20 @@ def apply_tickdir(self, tickdir): """ pass + def get_tickdir(self): + return self._tickdir + + def get_tick_padding(self): + """ + Get the length of the tick outside of the axes. + """ + padding = { + 'in': 0.0, + 'inout': 0.5, + 'out': 1.0 + } + return self._size * padding[self._tickdir] + def get_children(self): children = [self.tick1line, self.tick2line, self.gridline, self.label1, self.label2] @@ -349,13 +363,11 @@ def apply_tickdir(self, tickdir): if self._tickdir == 'in': self._tickmarkers = (mlines.TICKUP, mlines.TICKDOWN) - self._pad = self._base_pad elif self._tickdir == 'inout': self._tickmarkers = ('|', '|') - self._pad = self._base_pad + self._size / 2. else: self._tickmarkers = (mlines.TICKDOWN, mlines.TICKUP) - self._pad = self._base_pad + self._size + self._pad = self._base_pad + self.get_tick_padding() self.stale = True def _get_text1(self): @@ -485,13 +497,11 @@ def apply_tickdir(self, tickdir): if self._tickdir == 'in': self._tickmarkers = (mlines.TICKRIGHT, mlines.TICKLEFT) - self._pad = self._base_pad elif self._tickdir == 'inout': self._tickmarkers = ('_', '_') - self._pad = self._base_pad + self._size / 2. else: self._tickmarkers = (mlines.TICKLEFT, mlines.TICKRIGHT) - self._pad = self._base_pad + self._size + self._pad = self._base_pad + self.get_tick_padding() self.stale = True # how far from the y axis line the right of the ticklabel are @@ -1097,6 +1107,16 @@ def get_tightbbox(self, renderer): else: return None + def get_tick_padding(self): + values = [] + if len(self.majorTicks): + values.append(self.majorTicks[0].get_tick_padding()) + if len(self.minorTicks): + values.append(self.minorTicks[0].get_tick_padding()) + if len(values): + return max(values) + return 0.0 + @allow_rasterization def draw(self, renderer, *args, **kwargs): 'Draw the axis lines, grid lines, tick lines and labels' diff --git a/lib/matplotlib/tests/baseline_images/test_tightlayout/outward_ticks.pdf b/lib/matplotlib/tests/baseline_images/test_tightlayout/outward_ticks.pdf new file mode 100644 index 000000000000..7e0dd1ebcb58 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_tightlayout/outward_ticks.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_tightlayout/outward_ticks.png b/lib/matplotlib/tests/baseline_images/test_tightlayout/outward_ticks.png new file mode 100644 index 000000000000..cc73be432858 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_tightlayout/outward_ticks.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_tightlayout/outward_ticks.svg b/lib/matplotlib/tests/baseline_images/test_tightlayout/outward_ticks.svg new file mode 100644 index 000000000000..806cfe13c22b --- /dev/null +++ b/lib/matplotlib/tests/baseline_images/test_tightlayout/outward_ticks.svg @@ -0,0 +1,807 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/matplotlib/tests/test_tightlayout.py b/lib/matplotlib/tests/test_tightlayout.py index 9d43242ed9f5..588f66dbae65 100644 --- a/lib/matplotlib/tests/test_tightlayout.py +++ b/lib/matplotlib/tests/test_tightlayout.py @@ -159,6 +159,31 @@ def test_tight_layout8(): example_plot(ax, fontsize=24) +@image_comparison(baseline_images=['outward_ticks'], remove_text=True) +def test_outward_ticks(): + 'Test automatic use of tight_layout' + fig = plt.figure() + ax = fig.add_subplot(221) + ax.xaxis.set_tick_params(tickdir='out', length=16, width=3) + ax.yaxis.set_tick_params(tickdir='out', length=16, width=3) + ax.xaxis.set_tick_params( + tickdir='out', length=32, width=3, tick1On=True, which='minor') + ax.yaxis.set_tick_params( + tickdir='out', length=32, width=3, tick1On=True, which='minor') + ax.xaxis.set_ticks([0], minor=True) + ax.yaxis.set_ticks([0], minor=True) + ax = fig.add_subplot(222) + ax.xaxis.set_tick_params(tickdir='in', length=32, width=3) + ax.yaxis.set_tick_params(tickdir='in', length=32, width=3) + ax = fig.add_subplot(223) + ax.xaxis.set_tick_params(tickdir='inout', length=32, width=3) + ax.yaxis.set_tick_params(tickdir='inout', length=32, width=3) + ax = fig.add_subplot(224) + ax.xaxis.set_tick_params(tickdir='out', length=32, width=3) + ax.yaxis.set_tick_params(tickdir='out', length=32, width=3) + plt.tight_layout() + + def add_offsetboxes(ax, size=10, margin=.1, color='black'): """ Surround ax with OffsetBoxes