diff --git a/doc/users/next_whats_new/2018_01_30_tick_label_positions.rst b/doc/users/next_whats_new/2018_01_30_tick_label_positions.rst new file mode 100644 index 000000000000..ce402c3aafb2 --- /dev/null +++ b/doc/users/next_whats_new/2018_01_30_tick_label_positions.rst @@ -0,0 +1,9 @@ +Properties in `matplotlibrc` to place xasis and yaxis tick labels +------------------------------------------------------------------------------- + +Introducing four new boolean properties in `.matplotlibrc` for default +positions of xaxis and yaxis tick labels, namely, +`xtick.labeltop`, `xtick.labelbottom`, `ytick.labelright` and +`ytick.labelleft`. These can also be changed in rcParams. + + diff --git a/examples/ticks_and_spines/tick_label_right.py b/examples/ticks_and_spines/tick_label_right.py new file mode 100644 index 000000000000..8b4a8a9613f1 --- /dev/null +++ b/examples/ticks_and_spines/tick_label_right.py @@ -0,0 +1,31 @@ +""" +============================================ +Set default y-axis tick labels on the right +============================================ + +We can use :rc:`ytick.labelright` (default False) and :rc:`ytick.right` +(default False) and :rc:`ytick.labelleft` (default True) and :rc:`ytick.left` +(default True) to control where on the axes ticks and their labels appear. +These properties can also be set in the ``.matplotlib/matplotlibrc``. + +""" + + +import matplotlib.pyplot as plt +import numpy as np + +plt.rcParams['ytick.right'] = plt.rcParams['ytick.labelright'] = True +plt.rcParams['ytick.left'] = plt.rcParams['ytick.labelleft'] = False + + +x = np.arange(10) + +_, ax = plt.subplots(2, 1, sharex=True, figsize=(6, 6)) + +ax[0].plot(x) +ax[0].yaxis.tick_left() + +# use default parameter in rcParams, not calling tick_right() +ax[1].plot(x) + +plt.show() diff --git a/examples/ticks_and_spines/tick_xlabel_top.py b/examples/ticks_and_spines/tick_xlabel_top.py new file mode 100644 index 000000000000..d13ddea27a03 --- /dev/null +++ b/examples/ticks_and_spines/tick_xlabel_top.py @@ -0,0 +1,26 @@ +""" +========================================== +Set default x-axis tick labels on the top +========================================== + +We can use :rc:`xtick.labeltop` (default False) and :rc:`xtick.top` +(default False) and :rc:`xtick.labelbottom` (default True) and +:rc:`xtick.bottom` (default True) to control where on the axes ticks and +their labels appear. +These properties can also be set in the ``.matplotlib/matplotlibrc``. + +""" + + +import matplotlib.pyplot as plt +import numpy as np + + +plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False +plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True + +x = np.arange(10) + +plt.plot(x) +plt.title('xlabel top') +plt.show() diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index bb70972b6c51..d899ef5757e3 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -549,15 +549,31 @@ def __init__(self, fig, rect, self.tick_params( top=rcParams['xtick.top'] and rcParams['xtick.minor.top'], bottom=rcParams['xtick.bottom'] and rcParams['xtick.minor.bottom'], + labeltop=(rcParams['xtick.labeltop'] and + rcParams['xtick.minor.top']), + labelbottom=(rcParams['xtick.labelbottom'] and + rcParams['xtick.minor.bottom']), left=rcParams['ytick.left'] and rcParams['ytick.minor.left'], right=rcParams['ytick.right'] and rcParams['ytick.minor.right'], + labelleft=(rcParams['ytick.labelleft'] and + rcParams['ytick.minor.left']), + labelright=(rcParams['ytick.labelright'] and + rcParams['ytick.minor.right']), which='minor') self.tick_params( top=rcParams['xtick.top'] and rcParams['xtick.major.top'], bottom=rcParams['xtick.bottom'] and rcParams['xtick.major.bottom'], + labeltop=(rcParams['xtick.labeltop'] and + rcParams['xtick.major.top']), + labelbottom=(rcParams['xtick.labelbottom'] and + rcParams['xtick.major.bottom']), left=rcParams['ytick.left'] and rcParams['ytick.major.left'], right=rcParams['ytick.right'] and rcParams['ytick.major.right'], + labelleft=(rcParams['ytick.labelleft'] and + rcParams['ytick.major.left']), + labelright=(rcParams['ytick.labelright'] and + rcParams['ytick.major.right']), which='major') self._layoutbox = None diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index f6e33c05bfaa..6bbd9ead2de4 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -1234,6 +1234,8 @@ def _validate_linestyle(ls): # tick properties 'xtick.top': [False, validate_bool], # draw ticks on the top side 'xtick.bottom': [True, validate_bool], # draw ticks on the bottom side + 'xtick.labeltop': [False, validate_bool], # draw label on the top + 'xtick.labelbottom': [True, validate_bool], # draw label on the bottom 'xtick.major.size': [3.5, validate_float], # major xtick size in points 'xtick.minor.size': [2, validate_float], # minor xtick size in points 'xtick.major.width': [0.8, validate_float], # major xtick width in points @@ -1254,6 +1256,8 @@ def _validate_linestyle(ls): 'ytick.left': [True, validate_bool], # draw ticks on the left side 'ytick.right': [False, validate_bool], # draw ticks on the right side + 'ytick.labelleft': [True, validate_bool], # draw tick labels on the left side + 'ytick.labelright': [False, validate_bool], # draw tick labels on the right side 'ytick.major.size': [3.5, validate_float], # major ytick size in points 'ytick.minor.size': [2, validate_float], # minor ytick size in points 'ytick.major.width': [0.8, validate_float], # major ytick width in points diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 3e86a6296fa5..04ef89f736e3 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5224,6 +5224,36 @@ def test_axes_tick_params_gridlines(): assert axis.majorTicks[0]._grid_linestyle == 'dashdot' +def test_axes_tick_params_ylabelside(): + # Tests fix for issue 10267 + ax = plt.subplot() + ax.tick_params(labelleft=False, labelright=True, + which='major') + ax.tick_params(labelleft=False, labelright=True, + which='minor') + # expects left false, right true + assert ax.yaxis.majorTicks[0].label1On is False + assert ax.yaxis.majorTicks[0].label2On is True + assert ax.yaxis.minorTicks[0].label1On is False + assert ax.yaxis.minorTicks[0].label2On is True + + +def test_axes_tick_params_xlabelside(): + # Tests fix for issue 10267 + ax = plt.subplot() + ax.tick_params(labeltop=True, labelbottom=False, + which='major') + ax.tick_params(labeltop=True, labelbottom=False, + which='minor') + # expects top True, bottom False + # label1On mapped to labelbottom + # label2On mapped to labeltop + assert ax.xaxis.majorTicks[0].label1On is False + assert ax.xaxis.majorTicks[0].label2On is True + assert ax.xaxis.minorTicks[0].label1On is False + assert ax.xaxis.minorTicks[0].label2On is True + + def test_none_kwargs(): fig, ax = plt.subplots() ln, = ax.plot(range(32), linestyle=None) diff --git a/matplotlibrc.template b/matplotlibrc.template index 18c117e57453..feee0a72aa1d 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -357,6 +357,8 @@ backend : $TEMPLATE_BACKEND # see http://matplotlib.org/api/axis_api.html#matplotlib.axis.Tick #xtick.top : False # draw ticks on the top side #xtick.bottom : True # draw ticks on the bottom side +#xtick.labeltop : False # draw label on the top +#xtick.labelbottom : True # draw label on the bottom #xtick.major.size : 3.5 # major tick size in points #xtick.minor.size : 2 # minor tick size in points #xtick.major.width : 0.8 # major tick width in points @@ -374,6 +376,8 @@ backend : $TEMPLATE_BACKEND #ytick.left : True # draw ticks on the left side #ytick.right : False # draw ticks on the right side +#ytick.labelleft : True # draw tick labels on the left side +#ytick.labelright : False # draw tick labels on the right side #ytick.major.size : 3.5 # major tick size in points #ytick.minor.size : 2 # minor tick size in points #ytick.major.width : 0.8 # major tick width in points