diff --git a/doc/users/next_whats_new/2019-03-29-rcparam-axes-titlelocation.rst b/doc/users/next_whats_new/2019-03-29-rcparam-axes-titlelocation.rst new file mode 100644 index 000000000000..be99106c0d37 --- /dev/null +++ b/doc/users/next_whats_new/2019-03-29-rcparam-axes-titlelocation.rst @@ -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. diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 4630a7568818..25e2badb9c04 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -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. @@ -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. @@ -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()] diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 84cf4fc723cd..b7fbe01ea2a8 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -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) diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 1ecf4443ab1b..2e43dd627a96 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -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], @@ -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 diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 741a9c2b574a..b2471075dcf9 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -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') diff --git a/matplotlibrc.template b/matplotlibrc.template index c06ec1f8f9c3..d8351405327c 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -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 #axes.titlepad : 6.0 ## pad between axes and title in points