Skip to content

Add ytick label right/left properties in matplotlibrc #10306

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 16 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/users/next_whats_new/2018_01_30_tick_label_positions.rst
Original file line number Diff line number Diff line change
@@ -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.


31 changes: 31 additions & 0 deletions examples/ticks_and_spines/tick_label_right.py
Original file line number Diff line number Diff line change
@@ -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()
26 changes: 26 additions & 0 deletions examples/ticks_and_spines/tick_xlabel_top.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 16 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down