Skip to content

Feature request: set mathtext fonts via the object oriented API #6518

Closed
@ngoldbaum

Description

@ngoldbaum

As far as I can see (and according to the documentation), the only way to set the font of mathtext snippets in plots is via the mathtext.fontset rcparam.

It would be nice if there were some way to control the fonts used for mathtext rendering without messing with the rcparams system. Maybe the font could be chosen based on the FontProperties instance associated with the Text object? To make a concrete example, it would be nice if the y label for this plot were rendered using a serif font, like the rest of the plot.

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties

fig, ax = plt.subplots()

fp = FontProperties(**{'family': 'stixgeneral'})

im = ax.imshow(np.random.random((800, 800)))
cb = fig.colorbar(im)
cbax = cb.ax

ax.set_xlabel('x label')
ax.set_ylabel(r'$\rm{y\ label}$')
cb.set_label('colorbar label')

labels = ax.xaxis.get_ticklabels() + ax.yaxis.get_ticklabels()
labels += cbax.yaxis.get_ticklabels()
labels += [ax.xaxis.label, ax.yaxis.label, cbax.yaxis.label]

for label in labels:
    label.set_fontproperties(fp)

plt.savefig('test.png')

For additional context, see #6514 and my message to the matplotlib-devel mailing list: https://mail.python.org/pipermail/matplotlib-devel/2016-May/000454.html

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions