Skip to content

Commit 0ec4633

Browse files
authored
Merge pull request #10306 from esvhd/issue_10267
Add ytick label right/left properties in matplotlibrc
2 parents 5a75f5f + 145b409 commit 0ec4633

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Properties in `matplotlibrc` to place xasis and yaxis tick labels
2+
-------------------------------------------------------------------------------
3+
4+
Introducing four new boolean properties in `.matplotlibrc` for default
5+
positions of xaxis and yaxis tick labels, namely,
6+
`xtick.labeltop`, `xtick.labelbottom`, `ytick.labelright` and
7+
`ytick.labelleft`. These can also be changed in rcParams.
8+
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
============================================
3+
Set default y-axis tick labels on the right
4+
============================================
5+
6+
We can use :rc:`ytick.labelright` (default False) and :rc:`ytick.right`
7+
(default False) and :rc:`ytick.labelleft` (default True) and :rc:`ytick.left`
8+
(default True) to control where on the axes ticks and their labels appear.
9+
These properties can also be set in the ``.matplotlib/matplotlibrc``.
10+
11+
"""
12+
13+
14+
import matplotlib.pyplot as plt
15+
import numpy as np
16+
17+
plt.rcParams['ytick.right'] = plt.rcParams['ytick.labelright'] = True
18+
plt.rcParams['ytick.left'] = plt.rcParams['ytick.labelleft'] = False
19+
20+
21+
x = np.arange(10)
22+
23+
_, ax = plt.subplots(2, 1, sharex=True, figsize=(6, 6))
24+
25+
ax[0].plot(x)
26+
ax[0].yaxis.tick_left()
27+
28+
# use default parameter in rcParams, not calling tick_right()
29+
ax[1].plot(x)
30+
31+
plt.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
==========================================
3+
Set default x-axis tick labels on the top
4+
==========================================
5+
6+
We can use :rc:`xtick.labeltop` (default False) and :rc:`xtick.top`
7+
(default False) and :rc:`xtick.labelbottom` (default True) and
8+
:rc:`xtick.bottom` (default True) to control where on the axes ticks and
9+
their labels appear.
10+
These properties can also be set in the ``.matplotlib/matplotlibrc``.
11+
12+
"""
13+
14+
15+
import matplotlib.pyplot as plt
16+
import numpy as np
17+
18+
19+
plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False
20+
plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True
21+
22+
x = np.arange(10)
23+
24+
plt.plot(x)
25+
plt.title('xlabel top')
26+
plt.show()

lib/matplotlib/axes/_base.py

+16
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,31 @@ def __init__(self, fig, rect,
549549
self.tick_params(
550550
top=rcParams['xtick.top'] and rcParams['xtick.minor.top'],
551551
bottom=rcParams['xtick.bottom'] and rcParams['xtick.minor.bottom'],
552+
labeltop=(rcParams['xtick.labeltop'] and
553+
rcParams['xtick.minor.top']),
554+
labelbottom=(rcParams['xtick.labelbottom'] and
555+
rcParams['xtick.minor.bottom']),
552556
left=rcParams['ytick.left'] and rcParams['ytick.minor.left'],
553557
right=rcParams['ytick.right'] and rcParams['ytick.minor.right'],
558+
labelleft=(rcParams['ytick.labelleft'] and
559+
rcParams['ytick.minor.left']),
560+
labelright=(rcParams['ytick.labelright'] and
561+
rcParams['ytick.minor.right']),
554562
which='minor')
555563

556564
self.tick_params(
557565
top=rcParams['xtick.top'] and rcParams['xtick.major.top'],
558566
bottom=rcParams['xtick.bottom'] and rcParams['xtick.major.bottom'],
567+
labeltop=(rcParams['xtick.labeltop'] and
568+
rcParams['xtick.major.top']),
569+
labelbottom=(rcParams['xtick.labelbottom'] and
570+
rcParams['xtick.major.bottom']),
559571
left=rcParams['ytick.left'] and rcParams['ytick.major.left'],
560572
right=rcParams['ytick.right'] and rcParams['ytick.major.right'],
573+
labelleft=(rcParams['ytick.labelleft'] and
574+
rcParams['ytick.major.left']),
575+
labelright=(rcParams['ytick.labelright'] and
576+
rcParams['ytick.major.right']),
561577
which='major')
562578

563579
self._layoutbox = None

lib/matplotlib/rcsetup.py

+4
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,8 @@ def _validate_linestyle(ls):
12241224
# tick properties
12251225
'xtick.top': [False, validate_bool], # draw ticks on the top side
12261226
'xtick.bottom': [True, validate_bool], # draw ticks on the bottom side
1227+
'xtick.labeltop': [False, validate_bool], # draw label on the top
1228+
'xtick.labelbottom': [True, validate_bool], # draw label on the bottom
12271229
'xtick.major.size': [3.5, validate_float], # major xtick size in points
12281230
'xtick.minor.size': [2, validate_float], # minor xtick size in points
12291231
'xtick.major.width': [0.8, validate_float], # major xtick width in points
@@ -1244,6 +1246,8 @@ def _validate_linestyle(ls):
12441246

12451247
'ytick.left': [True, validate_bool], # draw ticks on the left side
12461248
'ytick.right': [False, validate_bool], # draw ticks on the right side
1249+
'ytick.labelleft': [True, validate_bool], # draw tick labels on the left side
1250+
'ytick.labelright': [False, validate_bool], # draw tick labels on the right side
12471251
'ytick.major.size': [3.5, validate_float], # major ytick size in points
12481252
'ytick.minor.size': [2, validate_float], # minor ytick size in points
12491253
'ytick.major.width': [0.8, validate_float], # major ytick width in points

lib/matplotlib/tests/test_axes.py

+30
Original file line numberDiff line numberDiff line change
@@ -5224,6 +5224,36 @@ def test_axes_tick_params_gridlines():
52245224
assert axis.majorTicks[0]._grid_linestyle == 'dashdot'
52255225

52265226

5227+
def test_axes_tick_params_ylabelside():
5228+
# Tests fix for issue 10267
5229+
ax = plt.subplot()
5230+
ax.tick_params(labelleft=False, labelright=True,
5231+
which='major')
5232+
ax.tick_params(labelleft=False, labelright=True,
5233+
which='minor')
5234+
# expects left false, right true
5235+
assert ax.yaxis.majorTicks[0].label1On is False
5236+
assert ax.yaxis.majorTicks[0].label2On is True
5237+
assert ax.yaxis.minorTicks[0].label1On is False
5238+
assert ax.yaxis.minorTicks[0].label2On is True
5239+
5240+
5241+
def test_axes_tick_params_xlabelside():
5242+
# Tests fix for issue 10267
5243+
ax = plt.subplot()
5244+
ax.tick_params(labeltop=True, labelbottom=False,
5245+
which='major')
5246+
ax.tick_params(labeltop=True, labelbottom=False,
5247+
which='minor')
5248+
# expects top True, bottom False
5249+
# label1On mapped to labelbottom
5250+
# label2On mapped to labeltop
5251+
assert ax.xaxis.majorTicks[0].label1On is False
5252+
assert ax.xaxis.majorTicks[0].label2On is True
5253+
assert ax.xaxis.minorTicks[0].label1On is False
5254+
assert ax.xaxis.minorTicks[0].label2On is True
5255+
5256+
52275257
def test_none_kwargs():
52285258
fig, ax = plt.subplots()
52295259
ln, = ax.plot(range(32), linestyle=None)

matplotlibrc.template

+4
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ backend : $TEMPLATE_BACKEND
357357
# see http://matplotlib.org/api/axis_api.html#matplotlib.axis.Tick
358358
#xtick.top : False # draw ticks on the top side
359359
#xtick.bottom : True # draw ticks on the bottom side
360+
#xtick.labeltop : False # draw label on the top
361+
#xtick.labelbottom : True # draw label on the bottom
360362
#xtick.major.size : 3.5 # major tick size in points
361363
#xtick.minor.size : 2 # minor tick size in points
362364
#xtick.major.width : 0.8 # major tick width in points
@@ -374,6 +376,8 @@ backend : $TEMPLATE_BACKEND
374376

375377
#ytick.left : True # draw ticks on the left side
376378
#ytick.right : False # draw ticks on the right side
379+
#ytick.labelleft : True # draw tick labels on the left side
380+
#ytick.labelright : False # draw tick labels on the right side
377381
#ytick.major.size : 3.5 # major tick size in points
378382
#ytick.minor.size : 2 # minor tick size in points
379383
#ytick.major.width : 0.8 # major tick width in points

0 commit comments

Comments
 (0)