简体   繁体   中英

Matplotlib custom styles: different tick color and tick label color

This is a somewhat a follow up on this question, where the author wants different tick and tick label colors. I want to have these settings saved as a Matplotlib style. But in Matplotlib styles, I can only set the tick color (see below for xtick settings), which changes tick and tick label color at the same time.

xtick.major.size     : 4      # major tick size in points
xtick.minor.size     : 4      # minor tick size in points
xtick.major.pad      : 6      # distance to major tick label in points
xtick.minor.pad      : 6      # distance to the minor tick label in points
xtick.color          : 555555      # color of the tick labels                   <---
xtick.labelsize      : medium # fontsize of the tick labels
xtick.direction : in # direction: in or out

In my company, we have grey ticks and black tick labels, how can I achieve this for all my plots?

thanks for your help kuzco

I had exactly the same issue as you have.

I have submitted a bug report to Matplotlib ( https://github.com/matplotlib/matplotlib/issues/17659 ) and wrote a PR to address this issue ( https://github.com/matplotlib/matplotlib/pull/17661 ). So, starting from Matplotlib v3.4.0 (not released yet, but available on the master branch now), this is fixed.

Previously, xtick.color used to define the tick color and the label color. The label color can now be set independently using xtick.labelcolor . It defaults to "inherit" which will take the value from xtick.color . The same holds for ytick.[label]color . For instance, to set the ticks to light grey and the tick labels to black, one can use the following code in a script:

import matplotlib as mpl

mpl.rcParams['xtick.labelcolor'] = 'lightgrey'
mpl.rcParams['xtick.color'] = 'black'
mpl.rcParams['ytick.labelcolor'] = 'lightgrey'
mpl.rcParams['ytick.color'] = 'black'

Or by adding the following lines to the matplotlib rc file or a matplotlib style file:

xtick.labelcolor : lightgrey
xtick.color      : black
ytick.labelcolor : lightgrey
ytick.color      : black

I hope this helps someone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM