8000 New rcparam to set default axes title location by yeosingng · Pull Request #13802 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

New rcparam to set default axes title location #13802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 11, 2019
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rcParam for default axes title location
---------------------------------------

A new rcParam value ``axes.titlelocation`` denotes the default axes title alignment.

Valid values are: left, center, and right.
7 changes: 5 additions & 2 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def get_title(self, loc="center"):
raise ValueError("'%s' is not a valid location" % loc)
return title.get_text()

def set_title(self, label, fontdict=None, loc="center", pad=None,
def set_title(self, label, fontdict=None, loc=None, pad=None,
**kwargs):
"""
Set a title for the axes.
Expand All @@ -150,7 +150,7 @@ def set_title(self, label, fontdict=None, loc="center", pad=None,
'horizontalalignment': loc}

loc : {'center', 'left', 'right'}, str, optional
Which title to set, defaults to 'center'
Which title to set, defaults to rcParams['axes.titlelocation']

pad : float
The offset of the title from the top of the axes, in points.
Expand All @@ -169,6 +169,9 @@ def set_title(self, label, fontdict=None, loc="center", pad=None,
properties.
"""
try:
if loc is None:
loc = rcParams['axes.titlelocation']

title = {'left': self._left_title,
'center': self.title,
'right': self._right_title}[loc.lower()]
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3019,7 +3019,7 @@ def sci(im):

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

Expand Down
3 changes: 3 additions & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,8 @@ def _validate_linestyle(ls):
"sequence.".format(ls))


validate_axes_titlelocation = ValidateInStrings('axes.titlelocation', ['left', 'center', 'right'])

# a map from key -> value, converter
defaultParams = {
'backend': [_auto_backend_sentinel, validate_backend],
Expand Down Expand Up @@ -1180,6 +1182,7 @@ def _validate_linestyle(ls):

'axes.titlesize': ['large', validate_fontsize], # fontsize of the
# axes title
'axes.titlelocation': ['center', validate_axes_titlelocation], # alignment of axes title
'axes.titleweight': ['normal', validate_string], # font weight of axes title
'axes.titlepad': [6.0, validate_float], # pad from axes top to title in points
'axes.grid': [False, validate_bool], # display grid or not
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5301,13 +5301,16 @@ def test_title_pad():

def test_title_location_roundtrip():
fig, ax = plt.subplots()
# set default title location
plt.rcParams['axes.titlelocation'] = 'center'

ax.set_title('aardvark')
ax.set_title('left', loc='left')
ax.set_title('right', loc='right')

assert 'left' == ax.get_title(loc='left')
assert 'right' == ax.get_title(loc='right')
assert 'aardvark' == ax.get_title()
assert 'aardvark' == ax.get_title(loc='center')

with pytest.raises(ValueError):
ax.get_title(loc='foo')
Expand Down
2 changes: 2 additions & 0 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@
#axes.grid : False ## display grid or not
#axes.grid.axis : both ## which axis the grid should apply to
#axes.grid.which : major ## gridlines at major, minor or both ticks
#axes.titlelocation : center ## alignment of the title,
## possible values are left, right and center
#axes.titlesize : large ## fontsize of the axes title
#axes.titleweight : normal ## font weight of title

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a very minor point here: I wouldn't put the location in between two parameters which set some font attributes. Maybe rather put it above size or below weight? Also the comment could mention the three possible values of this parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a valid point, I think above size would be a good location

#axes.titlepad : 6.0 ## pad between axes and title in points
Expand Down
0