From 23b3614e73c898afc73213137a66aff1fd0e7cfc Mon Sep 17 00:00:00 2001 From: yeo Date: Fri, 29 Mar 2019 12:35:08 -0400 Subject: [PATCH 1/7] added rcparam to allow users to set default axes title --- lib/matplotlib/axes/_axes.py | 6 ++- lib/matplotlib/rcsetup.py | 1 + matplotlibrc.template | 1 + setup.cfg.template | 79 ------------------------------------ 4 files changed, 7 insertions(+), 80 deletions(-) delete mode 100644 setup.cfg.template diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index affc329f1be0..657bbeaca2c0 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. @@ -169,6 +169,10 @@ 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/rcsetup.py b/lib/matplotlib/rcsetup.py index dd1f08120038..5bfc9cc2621e 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -1173,6 +1173,7 @@ def _validate_linestyle(ls): 'axes.titlesize': ['large', validate_fontsize], # fontsize of the # axes title + 'axes.titlelocation': ['center', validate_string], # 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/matplotlibrc.template b/matplotlibrc.template index df9ed2c7bcfc..634c0a1be766 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -295,6 +295,7 @@ #axes.grid.which : major ## gridlines at major, minor or both ticks #axes.titlesize : large ## fontsize of the axes title #axes.titleweight : normal ## font weight of title +#axes.titlelocation : center ## location of axes title #axes.titlepad : 6.0 ## pad between axes and title in points #axes.labelsize : medium ## fontsize of the x any y labels #axes.labelpad : 4.0 ## space between label and axis diff --git a/setup.cfg.template b/setup.cfg.template deleted file mode 100644 index 3b17cb94761d..000000000000 --- a/setup.cfg.template +++ /dev/null @@ -1,79 +0,0 @@ -# Rename this file to setup.cfg to modify Matplotlib's -# build options. - -[egg_info] - -[directories] -# Uncomment to override the default basedir in setupext.py. -# This can be a single directory or a comma-delimited list of directories. -#basedirlist = /usr - -[test] -# If you plan to develop Matplotlib and run or add to the test suite, -# set this to True. It will download and build a specific version of -# FreeType, and then use that to build the ft2font extension. This -# ensures that test images are exactly reproducible. -#local_freetype = False - -[status] -# To suppress display of the dependencies and their versions -# at the top of the build log, uncomment the following line: -#suppress = True - -[packages] -# There are a number of subpackages of Matplotlib that are considered -# optional. All except tests are installed by default, but that can -# be changed here. -# -#tests = False -#sample_data = True - -[gui_support] -# Matplotlib supports multiple GUI toolkits, including -# GTK3, MacOSX, Qt4, Qt5, Tk, and WX. Support for many of -# these toolkits requires AGG, the Anti-Grain Geometry library, -# which is provided by Matplotlib and built by default. -# -# Some backends are written in pure Python, and others require -# extension code to be compiled. By default, Matplotlib checks for -# these GUI toolkits during installation and, if present, compiles the -# required extensions to support the toolkit. -# -# - Tk support requires Tk development headers and Tkinter. -# - Mac OSX backend requires the Cocoa headers included with XCode. -# -# The other GUI toolkits do not require any extension code, and can be -# used as long as the libraries are installed on your system -- -# therefore they are installed unconditionally. -# -# You can uncomment any the following lines to change this -# behavior. Acceptable values are: -# -# True: build the extension. Exits with a warning if the -# required dependencies are not available -# False: do not build the extension -# auto: build if the required dependencies are available, -# otherwise skip silently. This is the default -# behavior -# -#agg = auto -#macosx = auto -#tkagg = auto - -[rc_options] -# User-configurable options -# -# Default backend, one of: Agg, Cairo, GTK3Agg, GTK3Cairo, MacOSX, Pdf, Ps, -# Qt4Agg, Qt5Agg, SVG, TkAgg, WX, WXAgg. -# -# The Agg, Ps, Pdf and SVG backends do not require external dependencies. Do -# not choose MacOSX, or TkAgg if you have disabled the relevant extension -# modules. The default is determined by fallback. -# -#backend = Agg - -[package_data] -# Package additional files found in the lib/matplotlib directories. -# -# On Windows, package DLL files. -#dlls = True From 0a0a7429e07860374cf928829935f7edde5a922a Mon Sep 17 00:00:00 2001 From: yeo Date: Fri, 29 Mar 2019 12:45:00 -0400 Subject: [PATCH 2/7] readded removed setup template and fixed spacing --- lib/matplotlib/axes/_axes.py | 1 - setup.cfg.template | 79 ++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 setup.cfg.template diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 657bbeaca2c0..e70fd8b38685 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -169,7 +169,6 @@ def set_title(self, label, fontdict=None, loc=None, pad=None, properties. """ try: - if loc is None: loc = rcParams['axes.titlelocation'] diff --git a/setup.cfg.template b/setup.cfg.template new file mode 100644 index 000000000000..3b17cb94761d --- /dev/null +++ b/setup.cfg.template @@ -0,0 +1,79 @@ +# Rename this file to setup.cfg to modify Matplotlib's +# build options. + +[egg_info] + +[directories] +# Uncomment to override the default basedir in setupext.py. +# This can be a single directory or a comma-delimited list of directories. +#basedirlist = /usr + +[test] +# If you plan to develop Matplotlib and run or add to the test suite, +# set this to True. It will download and build a specific version of +# FreeType, and then use that to build the ft2font extension. This +# ensures that test images are exactly reproducible. +#local_freetype = False + +[status] +# To suppress display of the dependencies and their versions +# at the top of the build log, uncomment the following line: +#suppress = True + +[packages] +# There are a number of subpackages of Matplotlib that are considered +# optional. All except tests are installed by default, but that can +# be changed here. +# +#tests = False +#sample_data = True + +[gui_support] +# Matplotlib supports multiple GUI toolkits, including +# GTK3, MacOSX, Qt4, Qt5, Tk, and WX. Support for many of +# these toolkits requires AGG, the Anti-Grain Geometry library, +# which is provided by Matplotlib and built by default. +# +# Some backends are written in pure Python, and others require +# extension code to be compiled. By default, Matplotlib checks for +# these GUI toolkits during installation and, if present, compiles the +# required extensions to support the toolkit. +# +# - Tk support requires Tk development headers and Tkinter. +# - Mac OSX backend requires the Cocoa headers included with XCode. +# +# The other GUI toolkits do not require any extension code, and can be +# used as long as the libraries are installed on your system -- +# therefore they are installed unconditionally. +# +# You can uncomment any the following lines to change this +# behavior. Acceptable values are: +# +# True: build the extension. Exits with a warning if the +# required dependencies are not available +# False: do not build the extension +# auto: build if the required dependencies are available, +# otherwise skip silently. This is the default +# behavior +# +#agg = auto +#macosx = auto +#tkagg = auto + +[rc_options] +# User-configurable options +# +# Default backend, one of: Agg, Cairo, GTK3Agg, GTK3Cairo, MacOSX, Pdf, Ps, +# Qt4Agg, Qt5Agg, SVG, TkAgg, WX, WXAgg. +# +# The Agg, Ps, Pdf and SVG backends do not require external dependencies. Do +# not choose MacOSX, or TkAgg if you have disabled the relevant extension +# modules. The default is determined by fallback. +# +#backend = Agg + +[package_data] +# Package additional files found in the lib/matplotlib directories. +# +# On Windows, package DLL files. +#dlls = True From 002ef2b78337c02b518e470123cd1ff2bbff4e45 Mon Sep 17 00:00:00 2001 From: yeo Date: Fri, 29 Mar 2019 13:20:51 -0400 Subject: [PATCH 3/7] added updated pyplot and modified spacing in matplotlibrc template --- lib/matplotlib/pyplot.py | 2 +- matplotlibrc.template | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 83e94feb31a8..5a7e8b3fd5cf 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -3036,7 +3036,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/matplotlibrc.template b/matplotlibrc.template index 634c0a1be766..ba4c1a50af7d 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -294,8 +294,8 @@ #axes.grid.axis : both ## which axis the grid should apply to #axes.grid.which : major ## gridlines at major, minor or both ticks #axes.titlesize : large ## fontsize of the axes title +#axes.titlelocation : center ## alignment of the title #axes.titleweight : normal ## font weight of title -#axes.titlelocation : center ## location of axes title #axes.titlepad : 6.0 ## pad between axes and title in points #axes.labelsize : medium ## fontsize of the x any y labels #axes.labelpad : 4.0 ## space between label and axis From 38a0f16f847c65d800afc41d8e607aa53c52487c Mon Sep 17 00:00:00 2001 From: yeo Date: Fri, 29 Mar 2019 15:28:36 -0400 Subject: [PATCH 4/7] moved location of new param in rc template, and modified doc in axes set_title --- lib/matplotlib/axes/_axes.py | 2 +- matplotlibrc.template | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 0a615c49b846..25e2badb9c04 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -150,7 +150,7 @@ def set_title(self, label, fontdict=None, loc=None, 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. diff --git a/matplotlibrc.template b/matplotlibrc.template index dadfff58d034..d8351405327c 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -293,8 +293,9 @@ #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.titlelocation : center ## alignment of the title #axes.titleweight : normal ## font weight of title #axes.titlepad : 6.0 ## pad between axes and title in points #axes.labelsize : medium ## fontsize of the x any y labels From fc659eae91f3d50cc8d9ec9b0b13e8fdda71dae3 Mon Sep 17 00:00:00 2001 From: yeo Date: Fri, 29 Mar 2019 15:53:54 -0400 Subject: [PATCH 5/7] changed validation so that only valid locations will be passed to axes.titlelocation --- lib/matplotlib/rcsetup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index 4e4045ac8f42..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,7 +1182,7 @@ def _validate_linestyle(ls): 'axes.titlesize': ['large', validate_fontsize], # fontsize of the # axes title - 'axes.titlelocation': ['center', validate_string], # alignment of 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 From 1c1fe6426f4942026652f4461fdad19cf1b92322 Mon Sep 17 00:00:00 2001 From: yeo Date: Fri, 29 Mar 2019 16:07:37 -0400 Subject: [PATCH 6/7] added a what's new for the new rcParam --- .../2019-03-29-rcparam-axes-titlelocation.rst | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 doc/users/next_whats_new/2019-03-29-rcparam-axes-titlelocation.rst 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. From 18fafb09c768ddb2af21071e424fa6e545082695 Mon Sep 17 00:00:00 2001 From: yeo Date: Fri, 29 Mar 2019 16:18:32 -0400 Subject: [PATCH 7/7] modified test_title_location_roundtrip to test default location using rcparams --- lib/matplotlib/tests/test_axes.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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')