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

Skip to content

Commit e960b5b

Browse files
committed
FIX: add None to axes.titlepad
1 parent 84286fc commit e960b5b

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,11 @@ 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']
174-
self._autotitlepos = None
175-
else:
176-
self._autotitlepos = False
177-
self._set_title_offset_trans(float(pad))
175+
self._set_title_offset_trans(pad)
176+
178177
title.set_text(label)
179178
title.update(default)
180179
if fontdict is not None:

lib/matplotlib/axes/_base.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,12 +1063,8 @@ 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:
1071-
self._autotitlepos = None
1066+
1067+
self._set_title_offset_trans(mpl.rcParams['axes.titlepad'])
10721068

10731069
for _title in (self.title, self._left_title, self._right_title):
10741070
self._set_artist_props(_title)
@@ -1120,13 +1116,20 @@ def set_facecolor(self, color):
11201116
self.stale = True
11211117
return self.patch.set_facecolor(color)
11221118

1123-
def _set_title_offset_trans(self, title_offset_points):
1119+
def _set_title_offset_trans(self, pad):
11241120
"""
11251121
Set the offset for the title either from :rc:`axes.titlepad`
11261122
or from set_title kwarg ``pad``.
11271123
"""
1124+
1125+
if pad is None:
1126+
self._autotitlepos = True
1127+
pad = 6 # default.
1128+
else:
1129+
self._autotitlepos = False
1130+
11281131
self.titleOffsetTrans = mtransforms.ScaledTranslation(
1129-
0.0, title_offset_points / 72,
1132+
0.0, pad / 72,
11301133
self.figure.dpi_scale_trans)
11311134
for _title in (self.title, self._left_title, self._right_title):
11321135
_title.set_transform(self.transAxes + self.titleOffsetTrans)

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 to allow autopadding.
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: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5477,9 +5477,6 @@ def test_title_noauto_pad():
54775477
bottom=True, top=True, labelbottom=True, labeltop=True)
54785478
fig.canvas.draw()
54795479
assert ax.title.get_position()[1] == 1.0
5480-
ax.set_title(f"Title 2")
5481-
fig.canvas.draw()
5482-
assert ax.title.get_position()[1] > 1.04
54835480

54845481

54855482
def test_offset_label_color():

matplotlibrc.template

Lines changed: 3 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,8 @@
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.
356+
# None means to autoplace the title.
356357
#axes.labelsize: medium # fontsize of the x any y labels
357358
#axes.labelpad: 4.0 # space between label and axis
358359
#axes.labelweight: normal # weight of the x and y labels

0 commit comments

Comments
 (0)