How to combine normal font and latex font types in Matplotlib?

47 Views Asked by At

I am aware of Matplotlib's math_fontfamily to mix family font types within the same string. I use the feature for my title and it works perfectly fine. For example:

ax[0].set_title(r'Total response $\tau_c/\tau$', math_fontfamily='cm')

mixes normal text and latex text.

Now, I would like to use the same feature for the plot legend. I am using the "label" command as in

ax[0].plot(x, y, label="I want normal text and latex text, eg $\tau/\gamma$")
ax[0].legend(loc='lower center')

How can I do this?

1

There are 1 best solutions below

1
JanS On

One possible option would be to enable Latex rendering via Matplotlib's rcParams, e.g.:

plt.rcParams.update({
    "text.usetex": True,
    "font.family": "Helvetica"
})

See the documentation for more info. Note, that this requires a working Latex installation.