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

Skip to content

Commit e4f7360

Browse files
committed
ENH/FIX: Don't auto reposition axes if axes.titlepad not None
Automatic title repositioning is now suppressed if the title padding is set to a non-None value by the user (either in the pad kwarg, or the rcParam axes.titlepad)
1 parent ef9fc20 commit e4f7360

File tree

7 files changed

+47
-13
lines changed

7 files changed

+47
-13
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ deprecation warning.
9898
`~.Axes.errorbar` now color cycles when only errorbar color is set
9999
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100100

101-
Previously setting the *ecolor* would turn off automatic color cycling for the plot, leading to the
102-
the lines and markers defaulting to whatever the first color in the color cycle was in the case of
103-
multiple plot calls.
101+
Previously setting the *ecolor* would turn off automatic color cycling for the plot, leading to the
102+
the lines and markers defaulting to whatever the first color in the color cycle was in the case of
103+
multiple plot calls.
104104

105105
`.rcsetup.validate_color_for_prop_cycle` now always raises TypeError for bytes input
106106
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -122,3 +122,14 @@ This behavior is consistent with other figure saving methods
122122
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123123
This is consistent with other signature mismatch errors. Previously a
124124
ValueError was raised.
125+
126+
:rc:`axes.titlepad` and *pad* argument of `~.Axes.set_title` now default to ``None``
127+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128+
129+
Since 3.0, Axes titles are automatically repositioned, primarily to avoid
130+
xlabels or xticks at the top of the axes. The user could turn this
131+
off using the optional *y* argument to `~.Axes.set_title`). However, that
132+
made it impossible to manually set the *pad* argument; Now *pad* defaults to
133+
``None`` and the rcParam :rc:`axes.titlepad` defaults to ``None``, which will
134+
allow automatic title placement. However, if these are set to a float, then
135+
the automatic placement of the title is turned off.

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,14 @@ def set_title(self, label, fontdict=None, loc=None, pad=None, **kwargs):
169169
titlecolor = rcParams['axes.titlecolor']
170170
if not cbook._str_lower_equal(titlecolor, 'auto'):
171171
default["color"] = titlecolor
172+
172173
if pad is None:
173174
pad = rcParams['axes.titlepad']
175+
if pad is not None:
176+
self._autotitlepos = False
177+
else:
178+
pad = 6 # default.
179+
174180
self._set_title_offset_trans(float(pad))
175181
title.set_text(label)
176182
title.update(default)

lib/matplotlib/axes/_base.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,11 +1063,9 @@ def cla(self):
10631063
verticalalignment='baseline',
10641064
horizontalalignment='right',
10651065
)
1066-
title_offset_points = mpl.rcParams['axes.titlepad']
1067-
# refactor this out so it can be called in ax.set_title if
1068-
# pad argument used...
1069-
self._set_title_offset_trans(title_offset_points)
1070-
# determine if the title position has been set manually:
1066+
# this needs to be called in case cla is not called on
1067+
# axes later...
1068+
self._set_title_offset_trans(mpl.rcParams['axes.titlepad'])
10711069
self._autotitlepos = None
10721070

10731071
for _title in (self.title, self._left_title, self._right_title):
@@ -1125,6 +1123,9 @@ def _set_title_offset_trans(self, title_offset_points):
11251123
Set the offset for the title either from :rc:`axes.titlepad`
11261124
or from set_title kwarg ``pad``.
11271125
"""
1126+
if title_offset_points is None:
1127+
# dummy default in case cla hasn't been called.
1128+
title_offset_points = 0
11281129
self.titleOffsetTrans = mtransforms.ScaledTranslation(
11291130
0.0, title_offset_points / 72,
11301131
self.figure.dpi_scale_trans)
@@ -2576,6 +2577,7 @@ def _update_title_position(self, renderer):
25762577
'already placed manually: %f', y)
25772578
return
25782579
self._autotitlepos = True
2580+
print('True!')
25792581

25802582
for title in titles:
25812583
x, _ = title.get_position()

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ def _convert_validator_spec(key, conv):
12131213
'axes.titlelocation': ['center', ['left', 'center', 'right']], # alignment of axes title
12141214
'axes.titleweight': ['normal', validate_fontweight], # font weight of axes title
12151215
'axes.titlecolor': ['auto', validate_color_or_auto], # font color of axes title
1216-
'axes.titlepad': [6.0, validate_float], # pad from axes top to title in points
1216+
'axes.titlepad': [None, validate_float_or_None], # pad from axes top to title in points; None means allow auto position of title
12171217
'axes.grid': [False, validate_bool], # display grid or not
12181218
'axes.grid.which': ['major', ['minor', 'both', 'major']], # set whether the grid is drawn on
12191219
# 'major' 'minor' or 'both' ticks

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5452,6 +5452,8 @@ def test_titlesetpos():
54525452
def test_title_xticks_top():
54535453
# Test that title moves if xticks on top of axes.
54545454
fig, ax = plt.subplots()
5455+
# set to the new default (from 5, which will suppress title reposition)
5456+
mpl.rcParams['axes.titlepad'] = None
54555457
ax.xaxis.set_ticks_position('top')
54565458
ax.set_title('xlabel top')
54575459
fig.canvas.draw()
@@ -5461,13 +5463,26 @@ def test_title_xticks_top():
54615463
def test_title_xticks_top_both():
54625464
# Test that title moves if xticks on top of axes.
54635465
fig, ax = plt.subplots()
5466+
# set to the new default (from 5, which will suppress title reposition)
5467+
mpl.rcParams['axes.titlepad'] = None
54645468
ax.tick_params(axis="x",
54655469
bottom=True, top=True, labelbottom=True, labeltop=True)
54665470
ax.set_title('xlabel top')
54675471
fig.canvas.draw()
54685472
assert ax.title.get_position()[1] > 1.04
54695473

54705474

5475+
def test_title_noauto_pad():
5476+
# test that if we pad, then the title does not autopos
5477+
5478+
fig, ax = plt.subplots()
5479+
ax.set_title(f"Title 1", pad=-20)
5480+
ax.tick_params(axis="x",
5481+
bottom=True, top=True, labelbottom=True, labeltop=True)
5482+
fig.canvas.draw()
5483+
assert ax.title.get_position()[1] == 1.0
5484+
5485+
54715486
def test_offset_label_color():
54725487
# Tests issue 6440
54735488
fig = plt.figure()

lib/matplotlib/tests/test_tightlayout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def example_plot(ax, fontsize=12):
1919
ax.set_title('Title', fontsize=fontsize)
2020

2121

22-
@image_comparison(['tight_layout1'])
22+
@image_comparison(['tight_layout1'], tol=1.87)
2323
def test_tight_layout1():
2424
"""Test tight_layout for a single subplot."""
2525
fig, ax = plt.subplots()
@@ -115,7 +115,7 @@ def test_tight_layout6():
115115
h_pad=0.45)
116116

117117

118-
@image_comparison(['tight_layout7'])
118+
@image_comparison(['tight_layout7'], tol=1.87)
119119
def test_tight_layout7():
120120
# tight layout with left and right titles
121121
fontsize = 24

matplotlibrc.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@
326326
#mathtext.sf: sans
327327
#mathtext.tt: monospace
328328
#mathtext.fallback: cm # Select fallback font from ['cm' (Computer Modern), 'stix'
329-
# 'stixsans'] when a symbol can not be found in one of the
329+
# 'stixsans'] when a symbol can not be found in one of the
330330
# custom math fonts. Select 'None' to not perform fallback
331331
# and replace the missing character by a dummy symbol.
332332
#mathtext.default: it # The default font to use for math.
@@ -352,7 +352,7 @@
352352
#axes.titleweight: normal # font weight of title
353353
#axes.titlecolor: auto # color of the axes title, auto falls back to
354354
# text.color as default value
355-
#axes.titlepad: 6.0 # pad between axes and title in points
355+
#axes.titlepad: None # pad between axes and title in points, None means allow automatic positioning
356356
#axes.labelsize: medium # fontsize of the x any y labels
357357
#axes.labelpad: 4.0 # space between label and axis
358358
#axes.labelweight: normal # weight of the x and y labels

0 commit comments

Comments
 (0)