Skip to content

Commit bf4259a

Browse files
committed
Tick label font family via tick_params
1 parent 4bdae2e commit bf4259a

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Allow setting the tick label fonts with a keyword argument
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
``Axes.tick_params`` now accepts a *labelfontfamily* keyword that changes the tick
4+
label font separately from the rest of the text objects:
5+
6+
.. code-block:: python
7+
8+
Axis.tick_params(labelfontfamily*='monospace')

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3337,6 +3337,8 @@ def tick_params(self, axis='both', **kwargs):
33373337
Tick label font size in points or as a string (e.g., 'large').
33383338
labelcolor : color
33393339
Tick label color.
3340+
labelfontfamily : str
3341+
Tick label font.
33403342
colors : color
33413343
Tick color and label color.
33423344
zorder : float

lib/matplotlib/axis.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(
6464
pad=None,
6565
labelsize=None,
6666
labelcolor=None,
67+
labelfontfamily=None,
6768
zorder=None,
6869
gridOn=None, # defaults to axes.grid depending on axes.grid.which
6970
tick1On=True,
@@ -174,11 +175,11 @@ def __init__(
174175
self.label1 = mtext.Text(
175176
np.nan, np.nan,
176177
fontsize=labelsize, color=labelcolor, visible=label1On,
177-
rotation=self._labelrotation[1])
178+
fontfamily=labelfontfamily, rotation=self._labelrotation[1])
178179
self.label2 = mtext.Text(
179180
np.nan, np.nan,
180181
fontsize=labelsize, color=labelcolor, visible=label2On,
181-
rotation=self._labelrotation[1])
182+
fontfamily=labelfontfamily, rotation=self._labelrotation[1])
182183

183184
self._apply_tickdir(tickdir)
184185

@@ -376,7 +377,7 @@ def _apply_params(self, **kwargs):
376377
self.label2.set(rotation=self._labelrotation[1])
377378

378379
label_kw = {k[5:]: v for k, v in kwargs.items()
379-
if k in ['labelsize', 'labelcolor']}
380+
if k in ['labelsize', 'labelcolor', 'labelfontfamily']}
380381
self.label1.set(**label_kw)
381382
self.label2.set(**label_kw)
382383

@@ -1036,7 +1037,7 @@ def _translate_tick_params(kw, reverse=False):
10361037
# The following lists may be moved to a more accessible location.
10371038
allowed_keys = [
10381039
'size', 'width', 'color', 'tickdir', 'pad',
1039-
'labelsize', 'labelcolor', 'zorder', 'gridOn',
1040+
'labelsize', 'labelcolor', 'labelfontfamily', 'zorder', 'gridOn',
10401041
'tick1On', 'tick2On', 'label1On', 'label2On',
10411042
'length', 'direction', 'left', 'bottom', 'right', 'top',
10421043
'labelleft', 'labelbottom', 'labelright', 'labeltop',

lib/matplotlib/axis.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ from matplotlib.ticker import Locator, Formatter
88
from matplotlib.transforms import Transform, Bbox
99

1010
import datetime
11-
from collections.abc import Callable, Iterable
11+
from collections.abc import Callable, Iterable, Sequence
1212
from typing import Any, Literal
1313
import numpy as np
1414
from numpy.typing import ArrayLike
@@ -36,6 +36,7 @@ class Tick(martist.Artist):
3636
pad: float | None = ...,
3737
labelsize: float | None = ...,
3838
labelcolor: ColorType | None = ...,
39+
labelfontfamily: str | Sequence[str] | None = ...,
3940
zorder: float | None = ...,
4041
gridOn: bool | None = ...,
4142
tick1On: bool = ...,

lib/matplotlib/tests/test_axes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8589,3 +8589,14 @@ def test_fill_between_axes_limits():
85898589
color='green', alpha=0.5, transform=ax.get_xaxis_transform())
85908590

85918591
assert (ax.get_xlim(), ax.get_ylim()) == original_lims
8592+
8593+
8594+
def test_tick_param_labelfont():
8595+
fig, ax = plt.subplots()
8596+
ax.plot([1, 2, 3, 4], [1, 2, 3, 4])
8597+
ax.set_xlabel('X label in Impact font', fontname='Impact')
8598+
ax.set_ylabel('Y label in Humor Sans', fontname='Humor Sans')
8599+
ax.tick_params(color='r', labelfontfamily='monospace')
8600+
plt.title('Title in sans-serif')
8601+
for text in ax.get_xticklabels():
8602+
assert text.get_fontfamily()[0] == 'monospace'

0 commit comments

Comments
 (0)