8000 Issue #17659: set tick color and tick labelcolor independently from r… · matplotlib/matplotlib@63893df · GitHub
[go: up one dir, main page]

Skip to content

Commit 63893df

Browse files
rbnvrwRuben Verweijjklymaktimhoffm
authored
Issue #17659: set tick color and tick labelcolor independently from rcParams (#17661)
* Issue #17659: set tick color and tick labelcolor independently from rcParams * Issue #17659: set tick color and tick labelcolor independently from rcParams, fix unit test * Implement comments on PR #17661 * Add blank lines for flake8 * More extensive whats-new for pr #17661 * Remove not needed check after changing to default value of 'inherit' for x/ytick.labelcolor * Update matplotlibrc.template Co-authored-by: Jody Klymak <jklymak@gmail.com> * Update matplotlibrc.template Co-authored-by: Jody Klymak <jklymak@gmail.com> * Apply suggestions from code review Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Update set_colors_ticks_and_ticklabels_rcparams.rst * Fix documentation header length * Make the 'whats new' information even more concise Co-authored-by: Ruben Verweij <ruben@lighthacking.nl> Co-authored-by: Jody Klymak <jklymak@gmail.com> Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
1 parent 7b9de23 commit 63893df

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
The color of ticks and tick labels can be set independently using rcParams
2+
--------------------------------------------------------------------------
3+
4+
Previously, :rc:`xtick.color` used to define the tick color and the label color.
5+
The label color can now be set independently using
6+
:rc:`xtick.labelcolor`. It defaults to "inherit" which will take the value
7+
from :rc:`xtick.color`. The same holds for ``ytick.[label]color``.
8+
For instance, to set the ticks to light grey and the tick labels
9+
to black, one can use the following code in a script::
10+
11+
12+
import matplotlib as mpl
13+
14+
mpl.rcParams['xtick.labelcolor'] = 'lightgrey'
15+
mpl.rcParams['xtick.color'] = 'black'
16+
mpl.rcParams['ytick.labelcolor'] = 'lightgrey'
17+
mpl.rcParams['ytick.color'] = 'black'
18+
19+
20+
Or by adding the following lines to the
21+
:ref:`matplotlib rc <customizing-with-matplotlibrc-files>` file: or a
22+
matplotlib style file:
23+
24+
25+
.. code-block:: none
26+
27+
xtick.labelcolor : lightgrey
28+
xtick.color : black
29+
ytick.labelcolor : lightgrey
30+
ytick.color : black

lib/matplotlib/axis.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def __init__(self, axes, loc, label=None,
120120
self._base_pad = pad
121121

122122
if labelcolor is None:
123+
labelcolor = mpl.rcParams[f"{name}.labelcolor"]
124+
125+
if labelcolor == 'inherit':
126+
# inherit from tick color
123127
labelcolor = mpl.rcParams[f"{name}.color"]
124128

125129
if labelsize is None:

lib/matplotlib/rcsetup.py

Lines changed: 6 additions & 2 deletions
-
"xtick.color": validate_color, # color of xtick labels
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,9 @@ def _convert_validator_spec(key, conv):
13201320
"xtick.minor.width": validate_float, # minor xtick width in points
13211321
"xtick.major.pad": validate_float, # distance to label in points
13221322
"xtick.minor.pad": validate_float, # distance to label in points
1323
1323+
"xtick.color": validate_color, # color of xticks
1324+
"xtick.labelcolor": validate_color_or_inherit,
1325+
# color of xtick labels
13241326
"xtick.minor.visible": validate_bool, # visibility of minor xticks
13251327
"xtick.minor.top": validate_bool, # draw top minor xticks
13261328
"xtick.minor.bottom": validate_bool, # draw bottom minor xticks
@@ -1340,7 +1342,9 @@ def _convert_validator_spec(key, conv):
13401342
"ytick.minor.width": validate_float, # minor ytick width in points
13411343
"ytick.major.pad": validate_float, # distance to label in points
13421344
"ytick.minor.pad": validate_float, # distance to label in points
1343-
"ytick.color": validate_color, # color of ytick labels
1345+
"ytick.color": validate_color, # color of yticks
1346+
"ytick.labelcolor": validate_color_or_inherit,
1347+
# color of ytick labels
13441348
"ytick.minor.visible": validate_bool, # visibility of minor yticks
13451349
"ytick.minor.left": validate_bool, # draw left minor yticks
13461350
"ytick.minor.right": validate_bool, # draw right minor yticks

lib/matplotlib/tests/test_axes.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6325,6 +6325,26 @@ def test_autoscale_tiny_sticky():
63256325
assert ax.get_ylim() == (0, 1.05e-9)
63266326

63276327

6328+
def test_xtickcolor_is_not_xticklabelcolor():
6329+
plt.rcParams['xtick.color'] = 'yellow'
6330+
plt.rcParams['xtick.labelcolor'] = 'blue'
6331+
ax = plt.axes()
6332+
ticks = ax.xaxis.get_major_ticks()
6333+
for tick in ticks:
6334+
assert tick.tick1line.get_color() == 'yellow'
6335+
assert tick.label1.get_color() == 'blue'
6336+
6337+
6338+
def test_ytickcolor_is_not_yticklabelcolor():
6339+
plt.rcParams['ytick.color'] = 'yellow'
6340+
plt.rcParams['ytick.labelcolor'] = 'blue'
6341+
ax = plt.axes()
6342+
ticks = ax.yaxis.get_major_ticks()
6343+
for tick in ticks:
6344+
assert tick.tick1line.get_color() == 'yellow'
6345+
assert tick.label1.get_color() == 'blue'
6346+
6347+
63286348
@pytest.mark.parametrize('size', [size for size in mfont_manager.font_scalings
63296349
if size is not None] + [8, 10, 12])
63306350
@pytest.mark.style('default')

matplotlibrc.template

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@
461461
#xtick.minor.width: 0.6 # minor tick width in points
462462
#xtick.major.pad: 3.5 # distance to major tick label in points
463463
#xtick.minor.pad: 3.4 # distance to the minor tick label in points
464-
#xtick.color: black # color of the tick labels
464+
#xtick.color: black # color of the ticks
465+
#xtick.labelcolor: inherit # color of the tick labels or inherit from xtick.color
465466
#xtick.labelsize: medium # fontsize of the tick labels
466467
#xtick.direction: out # direction: {in, out, inout}
467468
#xtick.minor.visible: False # visibility of minor ticks on x-axis
@@ -481,7 +482,8 @@
481482
#ytick.minor.width: 0.6 # minor tick width in points
482483
#ytick.major.pad: 3.5 # distance to major tick label in points
483484
#ytick.minor.pad: 3.4 # distance to the minor tick label in points
484-
#ytick.color: black # color of the tick labels
485+
#ytick.color: black # color of the ticks
486+
#ytick.labelcolor: inherit # color of the tick labels or inherit from ytick.color
485487
#ytick.labelsize: medium # fontsize of the tick labels
486488
#ytick.direction: out # direction: {in, out, inout}
487489
#ytick.minor.visible: False # visibility of minor ticks on y-axis

0 commit comments

Comments
 (0)
0