<p dir="auto">The weekly build with nightly wheels from numpy and pandas<br>
has failed. Check the logs for any updates that need to be<br>
made in matplotlib.<br>
<a href="https://github.com/matplotlib/matplotlib/actions/runs/2639783409">https://github.com/matplotlib/matplotlib/actions/runs/2639783409</a></p>
<p dir="auto">As far as I can see (and according to <a href="http://matplotlib.org/users/mathtext.html" rel="nofollow">the documentation</a>), the only way to set the font of mathtext snippets in plots is via the mathtext.fontset rcparam.</p>
<p dir="auto">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 <code class="notranslate">FontProperties</code> 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.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="
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')
"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">from</span> <span class="pl-s1">matplotlib</span> <span class="pl-k">import</span> <span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-k">from</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">font_manager</span> <span class="pl-k">import</span> <span class="pl-v">FontProperties</span>

<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>()

<span class="pl-s1">fp</span> <span class="pl-c1">=</span> <span class="pl-v">FontProperties</span>(<span class="pl-c1">**</span>{<span class="pl-s">'family'</span>: <span class="pl-s">'stixgeneral'</span>})

<span class="pl-s1">im</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">random</span>((<span class="pl-c1">800</span>, <span class="pl-c1">800</span>)))
<span class="pl-s1">cb</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">colorbar</span>(<span class="pl-s1">im</span>)
<span class="pl-s1">cbax</span> <span class="pl-c1">=</span> <span class="pl-s1">cb</span>.<span class="pl-s1">ax</span>

<span class="pl-s1">ax</span>.<span class="pl-en">set_xlabel</span>(<span class="pl-s">'x label'</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">set_ylabel</span>(<span class="pl-s">r'$\rm{y\ label}$'</span>)
<span class="pl-s1">cb</span>.<span class="pl-en">set_label</span>(<span class="pl-s">'colorbar label'</span>)

<span class="pl-s1">labels</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-s1">xaxis</span>.<span class="pl-en">get_ticklabels</span>() <span class="pl-c1">+</span> <span class="pl-s1">ax</span>.<span class="pl-s1">yaxis</span>.<span class="pl-en">get_ticklabels</span>()
<span class="pl-s1">labels</span> <span class="pl-c1">+=</span> <span class="pl-s1">cbax</span>.<span class="pl-s1">yaxis</span>.<span class="pl-en">get_ticklabels</span>()
<span class="pl-s1">labels</span> <span class="pl-c1">+=</span> [<span class="pl-s1">ax</span>.<span class="pl-s1">xaxis</span>.<span class="pl-s1">label</span>, <span class="pl-s1">ax</span>.<span class="pl-s1">yaxis</span>.<span class="pl-s1">label</span>, <span class="pl-s1">cbax</span>.<span class="pl-s1">yaxis</span>.<span class="pl-s1">label</span>]

<span class="pl-k">for</span> <span class="pl-s1">label</span> <span class="pl-c1">in</span> <span class="pl-s1">labels</span>:
    <span class="pl-s1">label</span>.<span class="pl-en">set_fontproperties</span>(<span class="pl-s1">fp</span>)

<span class="pl-s1">plt</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'test.png'</span>)</pre></div>
<p dir="auto">For additional context, see <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="157715875" data-permission-text="Title is private" data-url="https://github.com/matplotlib/matplotlib/issues/6514" data-hovercard-type="issue" data-hovercard-url="/matplotlib/matplotlib/issues/6514/hovercard" href="https://github.com/matplotlib/matplotlib/issues/6514">#6514</a> and my message to the matplotlib-devel mailing list: <a href="https://mail.python.org/pipermail/matplotlib-devel/2016-May/000454.html" rel="nofollow">https://mail.python.org/pipermail/matplotlib-devel/2016-May/000454.html</a></p>