Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4389baf

Browse filesBrowse files
authored
Merge pull request #9816 from jklymak/add-title-pad
ENH: add pad kwarg to set_title
2 parents 7ddc73a + d6fc866 commit 4389baf
Copy full SHA for 4389baf

File tree

Expand file treeCollapse file tree

4 files changed

+50
-7
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+50
-7
lines changed
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Add *pad* kwarg to ax.set_title
2+
-------------------------------
3+
4+
The method `axes.set_title` now has a *pad* kwarg, that specifies the
5+
distance from the top of an axes to where the title is drawn. The units
6+
of *pad* is points, and the default is the value of the (already-existing)
7+
``rcParams['axes.titlepad']``.

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import functools
88
import itertools
9+
import logging
910
import math
1011
import warnings
1112

@@ -42,6 +43,7 @@
4243
from matplotlib.container import BarContainer, ErrorbarContainer, StemContainer
4344
from matplotlib.axes._base import _AxesBase, _process_plot_format
4445

46+
_log = logging.getLogger(__name__)
4547

4648
rcParams = matplotlib.rcParams
4749

@@ -134,7 +136,8 @@ def get_title(self, loc="center"):
134136
raise ValueError("'%s' is not a valid location" % loc)
135137
return title.get_text()
136138

137-
def set_title(self, label, fontdict=None, loc="center", **kwargs):
139+
def set_title(self, label, fontdict=None, loc="center", pad=None,
140+
**kwargs):
138141
"""
139142
Set a title for the axes.
140143
@@ -159,6 +162,10 @@ def set_title(self, label, fontdict=None, loc="center", **kwargs):
159162
loc : {'center', 'left', 'right'}, str, optional
160163
Which title to set, defaults to 'center'
161164
165+
pad : float
166+
The offset of the title from the top of the axes, in points.
167+
Default is ``None`` to use rcParams['axes.titlepad'].
168+
162169
Returns
163170
-------
164171
text : :class:`~matplotlib.text.Text`
@@ -182,6 +189,9 @@ def set_title(self, label, fontdict=None, loc="center", **kwargs):
182189
'fontweight': rcParams['axes.titleweight'],
183190
'verticalalignment': 'baseline',
184191
'horizontalalignment': loc.lower()}
192+
if pad is None:
193+
pad = rcParams['axes.titlepad']
194+
self._set_title_offset_trans(float(pad))
185195
title.set_text(label)
186196
title.update(default)
187197
if fontdict is not None:

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+16-6Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,10 +1053,6 @@ def cla(self):
10531053
size=rcParams['axes.titlesize'],
10541054
weight=rcParams['axes.titleweight'])
10551055

1056-
title_offset_points = rcParams['axes.titlepad']
1057-
self.titleOffsetTrans = mtransforms.ScaledTranslation(
1058-
0.0, title_offset_points / 72.0,
1059-
self.figure.dpi_scale_trans)
10601056
self.title = mtext.Text(
10611057
x=0.5, y=1.0, text='',
10621058
fontproperties=props,
@@ -1074,10 +1070,12 @@ def cla(self):
10741070
verticalalignment='baseline',
10751071
horizontalalignment='right',
10761072
)
1073+
title_offset_points = rcParams['axes.titlepad']
1074+
# refactor this out so it can be called in ax.set_title if
1075+
# pad argument used...
1076+
self._set_title_offset_trans(title_offset_points)
10771077

10781078
for _title in (self.title, self._left_title, self._right_title):
1079-
_title.set_transform(self.transAxes + self.titleOffsetTrans)
1080-
_title.set_clip_box(None)
10811079
self._set_artist_props(_title)
10821080

10831081
# The patch draws the background of the axes. We want this to be below
@@ -1134,6 +1132,18 @@ def set_facecolor(self, color):
11341132
return self.patch.set_facecolor(color)
11351133
set_fc = set_facecolor
11361134

1135+
def _set_title_offset_trans(self, title_offset_points):
1136+
"""
1137+
Set the offset for the title either from rcParams['axes.titlepad']
1138+
or from set_title kwarg ``pad``.
1139+
"""
1140+
self.titleOffsetTrans = mtransforms.ScaledTranslation(
1141+
0.0, title_offset_points / 72.0,
1142+
self.figure.dpi_scale_trans)
1143+
for _title in (self.title, self._left_title, self._right_title):
1144+
_title.set_transform(self.transAxes + self.titleOffsetTrans)
1145+
_title.set_clip_box(None)
1146+
11371147
def set_prop_cycle(self, *args, **kwargs):
11381148
"""
11391149
Set the property cycle for any future plot commands on this Axes.

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4917,6 +4917,22 @@ def test_dash_offset():
49174917
ax.plot(x, j*y, ls=(j, (10, 10)), lw=5, color='k')
49184918

49194919

4920+
def test_title_pad():
4921+
# check that title padding puts the title in the right
4922+
# place...
4923+
fig, ax = plt.subplots()
4924+
ax.set_title('aardvark', pad=30.)
4925+
m = ax.titleOffsetTrans.get_matrix()
4926+
assert m[1, -1] == (30. / 72. * fig.dpi)
4927+
ax.set_title('aardvark', pad=0.)
4928+
m = ax.titleOffsetTrans.get_matrix()
4929+
assert m[1, -1] == 0.
4930+
# check that it is reverted...
4931+
ax.set_title('aardvark', pad=None)
4932+
m = ax.titleOffsetTrans.get_matrix()
4933+
assert m[1, -1] == (matplotlib.rcParams['axes.titlepad'] / 72. * fig.dpi)
4934+
4935+
49204936
def test_title_location_roundtrip():
49214937
fig, ax = plt.subplots()
49224938
ax.set_title('aardvark')

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.