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

Skip to content

Commit dd18211

Browse files
authored
Merge pull request #13802 from yeosingng/default-titleloc
New rcparam to set default axes title location
2 parents b9fa7b7 + 18fafb0 commit dd18211

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
rcParam for default axes title location
2+
---------------------------------------
3+
4+
A new rcParam value ``axes.titlelocation`` denotes the default axes title alignment.
5+
6+
Valid values are: left, center, and right.

lib/matplotlib/axes/_axes.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def get_title(self, loc="center"):
147147
raise ValueError("'%s' is not a valid location" % loc)
148148
return title.get_text()
149149

150-
def set_title(self, label, fontdict=None, loc="center", pad=None,
150+
def set_title(self, label, fontdict=None, loc=None, pad=None,
151151
**kwargs):
152152
"""
153153
Set a title for the axes.
@@ -171,7 +171,7 @@ def set_title(self, label, fontdict=None, loc="center", pad=None,
171171
'horizontalalignment': loc}
172172
173173
loc : {'center', 'left', 'right'}, str, optional
174-
Which title to set, defaults to 'center'
174+
Which title to set, defaults to rcParams['axes.titlelocation']
175175
176176
pad : float
177177
The offset of the title from the top of the axes, in points.
@@ -190,6 +190,9 @@ def set_title(self, label, fontdict=None, loc="center", pad=None,
190190
properties.
191191
"""
192192
try:
193+
if loc is None:
194+
loc = rcParams['axes.titlelocation']
195+
193196
title = {'left': self._left_title,
194197
'center': self.title,
195198
'right': self._right_title}[loc.lower()]

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3037,7 +3037,7 @@ def sci(im):
30373037

30383038
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
30393039
@docstring.copy(Axes.set_title)
3040-
def title(label, fontdict=None, loc='center', pad=None, **kwargs):
3040+
def title(label, fontdict=None, loc=None, pad=None, **kwargs):
30413041
return gca().set_title(
30423042
label, fontdict=fontdict, loc=loc, pad=pad, **kwargs)
30433043

lib/matplotlib/rcsetup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,8 @@ def _validate_linestyle(ls):
10081008
"sequence.".format(ls))
10091009

10101010

1011+
validate_axes_titlelocation = ValidateInStrings('axes.titlelocation', ['left', 'center', 'right'])
1012+
10111013
# a map from key -> value, converter
10121014
defaultParams = {
10131015
'backend': [_auto_backend_sentinel, validate_backend],
@@ -1190,6 +1192,7 @@ def _validate_linestyle(ls):
11901192

11911193
'axes.titlesize': ['large', validate_fontsize], # fontsize of the
11921194
# axes title
1195+
'axes.titlelocation': ['center', validate_axes_titlelocation], # alignment of axes title
11931196
'axes.titleweight': ['normal', validate_string], # font weight of axes title
11941197
'axes.titlepad': [6.0, validate_float], # pad from axes top to title in points
11951198
'axes.grid': [False, validate_bool], # display grid or not

lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5301,13 +5301,16 @@ def test_title_pad():
53015301

53025302
def test_title_location_roundtrip():
53035303
fig, ax = plt.subplots()
5304+
# set default title location
5305+
plt.rcParams['axes.titlelocation'] = 'center'
5306+
53045307
ax.set_title('aardvark')
53055308
ax.set_title('left', loc='left')
53065309
ax.set_title('right', loc='right')
53075310

53085311
assert 'left' == ax.get_title(loc='left')
53095312
assert 'right' == ax.get_title(loc='right')
5310-
assert 'aardvark' == ax.get_title()
5313+
assert 'aardvark' == ax.get_title(loc='center')
53115314

53125315
with pytest.raises(ValueError):
53135316
ax.get_title(loc='foo')

matplotlibrc.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@
293293
#axes.grid : False ## display grid or not
294294
#axes.grid.axis : both ## which axis the grid should apply to
295295
#axes.grid.which : major ## gridlines at major, minor or both ticks
296+
#axes.titlelocation : center ## alignment of the title,
297+
## possible values are left, right and center
296298
#axes.titlesize : large ## fontsize of the axes title
297299
#axes.titleweight : normal ## font weight of title
298300
#axes.titlepad : 6.0 ## pad between axes and title in points

0 commit comments

Comments
 (0)