Skip to content

Commit 2466ccb

Browse files
authored
Merge pull request matplotlib#25746 from saranti/labelfont
Tick label font family via tick_params
2 parents 55d90d2 + 76ec481 commit 2466ccb

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

@@ -378,7 +379,7 @@ def _apply_params(self, **kwargs):
378379
self.label2.set(rotation=self._labelrotation[1])
379380

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

@@ -1038,7 +1039,7 @@ def _translate_tick_params(kw, reverse=False):
10381039
# The following lists may be moved to a more accessible location.
10391040
allowed_keys = [
10401041
'size', 'width', 'color', 'tickdir', 'pad',
1041-
'labelsize', 'labelcolor', 'zorder', 'gridOn',
1042+
'labelsize', 'labelcolor', 'labelfontfamily', 'zorder', 'gridOn',
10421043
'tick1On', 'tick2On', 'label1On', 'label2On',
10431044
'length', 'direction', 'left', 'bottom', 'right', 'top',
10441045
'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
@@ -8656,3 +8656,14 @@ def test_fill_between_axes_limits():
86568656
color='green', alpha=0.5, transform=ax.get_xaxis_transform())
86578657

86588658
assert (ax.get_xlim(), ax.get_ylim()) == original_lims
8659+
8660+
8661+
def test_tick_param_labelfont():
8662+
fig, ax = plt.subplots()
8663+
ax.plot([1, 2, 3, 4], [1, 2, 3, 4])
8664+
ax.set_xlabel('X label in Impact font', fontname='Impact')
8665+
ax.set_ylabel('Y label in Humor Sans', fontname='Humor Sans')
8666+
ax.tick_params(color='r', labelfontfamily='monospace')
8667+
plt.title('Title in sans-serif')
8668+
for text in ax.get_xticklabels():
8669+
assert text.get_fontfamily()[0] == 'monospace'

0 commit comments

Comments
 (0)