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

Skip to content

Commit 7c9ab29

Browse files
committed
Move title if x-axis is on the top of the figure
Autoplace title now Small cleanup fixed case where no tick pos P8 Works for twins Added image test P8 Fix padding Add test for not auto moving.
1 parent 5eb185f commit 7c9ab29

File tree

6 files changed

+838
-4
lines changed

6 files changed

+838
-4
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from six.moves import xrange
88

99
import itertools
10+
import logging
1011
import warnings
1112
import math
1213
from operator import attrgetter
@@ -508,6 +509,9 @@ def __init__(self, fig, rect,
508509
# warnings.warn(
509510
# 'shared axes: "adjustable" is being changed to "datalim"')
510511
self._adjustable = 'datalim'
512+
# list of possible twinned axes...
513+
self._twinx_axes = []
514+
self._twiny_axes = []
511515
self.set_label(label)
512516
self.set_figure(fig)
513517

@@ -1063,6 +1067,8 @@ def cla(self):
10631067
verticalalignment='baseline',
10641068
horizontalalignment='right',
10651069
)
1070+
# determine if the title position has been set manually:
1071+
self._autotitlepos = None
10661072

10671073
for _title in (self.title, self._left_title, self._right_title):
10681074
_title.set_transform(self.transAxes + self.titleOffsetTrans)
@@ -2315,11 +2321,59 @@ def handle_single_axis(scale, autoscaleon, shared_axes, interval,
23152321
def _get_axis_list(self):
23162322
return (self.xaxis, self.yaxis)
23172323

2324+
def _update_title_position(self, renderer):
2325+
"""
2326+
Update the title position based on the bounding box enclosing
2327+
all the ticklabels and x-axis spine and xlabel...
2328+
"""
2329+
log = logging.getLogger(__name__)
2330+
log.debug('update_title_pos')
2331+
2332+
if self._autotitlepos is not None and not self._autotitlepos:
2333+
log.info('title position was updated manually, not adjusting')
2334+
return
2335+
2336+
titles = (self.title, self._left_title, self._right_title)
2337+
2338+
if self._autotitlepos is None:
2339+
for title in titles:
2340+
x, y = title.get_position()
2341+
if not np.isclose(y, 1.0):
2342+
self._autotitlepos = False
2343+
log.info('not adjusting title pos because title was'
2344+
' already placed manually: %f', y)
2345+
return
2346+
self._autotitlepos = True
2347+
2348+
for title in titles:
2349+
x, y0 = title.get_position()
2350+
y = 1.0
2351+
# need to check all our twins too...
2352+
axs = [self] + self._twinx_axes + self._twiny_axes
2353+
2354+
for ax in axs:
2355+
try:
2356+
if (ax.xaxis.get_label_position() == 'top'
2357+
or ax.xaxis.get_tick_position() == 'top'):
2358+
# this may not work?
2359+
bb = ax.xaxis.get_tightbbox(renderer)
2360+
top = bb.y1
2361+
# we don't need to pad because the padding is already
2362+
# in __init__: titleOffsetTrans
2363+
yn = self.transAxes.inverted().transform((0., top))[1]
2364+
if yn > y:
2365+
y = yn
2366+
except AttributeError:
2367+
pass
2368+
2369+
title.set_position((x, y))
2370+
23182371
# Drawing
23192372

23202373
@allow_rasterization
23212374
def draw(self, renderer=None, inframe=False):
23222375
"""Draw everything (plot lines, axes, labels)"""
2376+
log = logging.getLogger(__name__)
23232377
if renderer is None:
23242378
renderer = self._cachedRenderer
23252379

@@ -2328,6 +2382,7 @@ def draw(self, renderer=None, inframe=False):
23282382
if not self.get_visible():
23292383
return
23302384
renderer.open_group('axes')
2385+
23312386
# prevent triggering call backs during the draw process
23322387
self._stale = True
23332388
locator = self.get_axes_locator()
@@ -2348,6 +2403,8 @@ def draw(self, renderer=None, inframe=False):
23482403
for spine in six.itervalues(self.spines):
23492404
artists.remove(spine)
23502405

2406+
self._update_title_position(renderer)
2407+
23512408
if self.axison and not inframe:
23522409
if self._axisbelow is True:
23532410
self.xaxis.set_zorder(0.5)
@@ -2376,6 +2433,8 @@ def draw(self, renderer=None, inframe=False):
23762433
# rasterize artists with negative zorder
23772434
# if the minimum zorder is negative, start rasterization
23782435
rasterization_zorder = self._rasterization_zorder
2436+
log.debug('rasterization_zorder %s', rasterization_zorder)
2437+
23792438
if (rasterization_zorder is not None and
23802439
artists and artists[0].zorder < rasterization_zorder):
23812440
renderer.start_rasterizing()
@@ -2396,7 +2455,10 @@ def draw(self, renderer=None, inframe=False):
23962455
a.draw(renderer)
23972456
renderer.stop_rasterizing()
23982457

2458+
for a in artists:
2459+
log.debug('draw: artist %s', a)
23992460
mimage._draw_list_compositing_images(renderer, self, artists)
2461+
self._update_title_position(renderer)
24002462

24012463
renderer.close_group('axes')
24022464
self._cachedRenderer = renderer
@@ -3949,6 +4011,12 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
39494011
else:
39504012
self.apply_aspect()
39514013

4014+
bb_xaxis = self.xaxis.get_tightbbox(renderer)
4015+
if bb_xaxis:
4016+
bb.append(bb_xaxis)
4017+
4018+
self._update_title_position(renderer)
4019+
39524020
bb.append(self.get_window_extent(renderer))
39534021

39544022
if self.title.get_visible():
@@ -3958,10 +4026,6 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
39584026
if self._right_title.get_visible():
39594027
bb.append(self._right_title.get_window_extent(renderer))
39604028

3961-
bb_xaxis = self.xaxis.get_tightbbox(renderer)
3962-
if bb_xaxis:
3963-
bb.append(bb_xaxis)
3964-
39654029
bb_yaxis = self.yaxis.get_tightbbox(renderer)
39664030
if bb_yaxis:
39674031
bb.append(bb_yaxis)
@@ -4010,6 +4074,7 @@ def twinx(self):
40104074
self.yaxis.tick_left()
40114075
ax2.xaxis.set_visible(False)
40124076
ax2.patch.set_visible(False)
4077+
self._twinx_axes.append(ax2)
40134078
return ax2
40144079

40154080
def twiny(self):
@@ -4039,6 +4104,7 @@ def twiny(self):
40394104
self.xaxis.tick_bottom()
40404105
ax2.yaxis.set_visible(False)
40414106
ax2.patch.set_visible(False)
4107+
self._twiny_axes.append(ax2)
40424108
return ax2
40434109

40444110
def get_shared_x_axes(self):

lib/matplotlib/axis.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import six
88

9+
import logging
10+
911
from matplotlib import rcParams
1012
import matplotlib.artist as artist
1113
from matplotlib.artist import allow_rasterization
Binary file not shown.

0 commit comments

Comments
 (0)