Skip to content

Tick label font family via tick_params #25746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/users/next_whats_new/tick_labelfont_param.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Allow setting the tick label fonts with a keyword argument
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``Axes.tick_params`` now accepts a *labelfontfamily* keyword that changes the tick
label font separately from the rest of the text objects:

.. code-block:: python

Axis.tick_params(labelfontfamily='monospace')
2 changes: 2 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3337,6 +3337,8 @@ def tick_params(self, axis='both', **kwargs):
Tick label font size in points or as a string (e.g., 'large').
labelcolor : color
Tick label color.
labelfontfamily : str
Tick label font.
colors : color
Tick color and label color.
zorder : float
Expand Down
9 changes: 5 additions & 4 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(
pad=None,
labelsize=None,
labelcolor=None,
labelfontfamily=None,
zorder=None,
gridOn=None, # defaults to axes.grid depending on axes.grid.which
tick1On=True,
Expand Down Expand Up @@ -174,11 +175,11 @@ def __init__(
self.label1 = mtext.Text(
np.nan, np.nan,
fontsize=labelsize, color=labelcolor, visible=label1On,
rotation=self._labelrotation[1])
fontfamily=labelfontfamily, rotation=self._labelrotation[1])
self.label2 = mtext.Text(
np.nan, np.nan,
fontsize=labelsize, color=labelcolor, visible=label2On,
rotation=self._labelrotation[1])
fontfamily=labelfontfamily, rotation=self._labelrotation[1])

self._apply_tickdir(tickdir)

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

label_kw = {k[5:]: v for k, v in kwargs.items()
if k in ['labelsize', 'labelcolor']}
if k in ['labelsize', 'labelcolor', 'labelfontfamily']}
self.label1.set(**label_kw)
self.label2.set(**label_kw)

Expand Down Expand Up @@ -1036,7 +1037,7 @@ def _translate_tick_params(kw, reverse=False):
# The following lists may be moved to a more accessible location.
allowed_keys = [
'size', 'width', 'color', 'tickdir', 'pad',
'labelsize', 'labelcolor', 'zorder', 'gridOn',
'labelsize', 'labelcolor', 'labelfontfamily', 'zorder', 'gridOn',
'tick1On', 'tick2On', 'label1On', 'label2On',
'length', 'direction', 'left', 'bottom', 'right', 'top',
'labelleft', 'labelbottom', 'labelright', 'labeltop',
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/axis.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from matplotlib.ticker import Locator, Formatter
from matplotlib.transforms import Transform, Bbox

import datetime
from collections.abc import Callable, Iterable
from collections.abc import Callable, Iterable, Sequence
from typing import Any, Literal
import numpy as np
from numpy.typing import ArrayLike
Expand Down Expand Up @@ -36,6 +36,7 @@ class Tick(martist.Artist):
pad: float | None = ...,
labelsize: float | None = ...,
labelcolor: ColorType | None = ...,
labelfontfamily: str | Sequence[str] | None = ...,
zorder: float | None = ...,
gridOn: bool | None = ...,
tick1On: bool = ...,
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8589,3 +8589,14 @@ def test_fill_between_axes_limits():
color='green', alpha=0.5, transform=ax.get_xaxis_transform())

assert (ax.get_xlim(), ax.get_ylim()) == original_lims


def test_tick_param_labelfont():
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [1, 2, 3, 4])
ax.set_xlabel('X label in Impact font', fontname='Impact')
ax.set_ylabel('Y label in Humor Sans', fontname='Humor Sans')
ax.tick_params(color='r', labelfontfamily='monospace')
plt.title('Title in sans-serif')
for text in ax.get_xticklabels():
assert text.get_fontfamily()[0] == 'monospace'