Skip to content

Commit d00c7e0

Browse files
committed
FIX
1 parent ef9fc20 commit d00c7e0

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

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: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,11 +1063,7 @@ 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+
10711067
self._autotitlepos = None
10721068

10731069
for _title in (self.title, self._left_title, self._right_title):

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5452,6 +5452,7 @@ 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+
mpl.rcParams['axes.titlepad'] = None
54555456
ax.xaxis.set_ticks_position('top')
54565457
ax.set_title('xlabel top')
54575458
fig.canvas.draw()
@@ -5461,13 +5462,25 @@ def test_title_xticks_top():
54615462
def test_title_xticks_top_both():
54625463
# Test that title moves if xticks on top of axes.
54635464
fig, ax = plt.subplots()
5465+
mpl.rcParams['axes.titlepad'] = None
54645466
ax.tick_params(axis="x",
54655467
bottom=True, top=True, labelbottom=True, labeltop=True)
54665468
ax.set_title('xlabel top')
54675469
fig.canvas.draw()
54685470
assert ax.title.get_position()[1] > 1.04
54695471

54705472

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

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)