I want to use a subscript in an axis label in a matplotlib figure. Using LaTeX I would set it as $N_i$
, which gives me the italic serif font. I know I can get non-italic mathfont with \mathrm
. But I would like to get the text in the default matplotlib sans-serif font so it matches the rest of the text in the figure. Is there a way to subscript text without using latex?
32
votes
3 Answers
35
votes
Use \mathregular
to use the font used for regular text outside of mathtext:
$\mathregular{N_i}$
Take a look here for more information.
29
votes
You can do it by customizing rcParams
. If you have multiple elements to customize, you can store them as a dict
and the update the `rcParams':
params = {'mathtext.default': 'regular' }
plt.rcParams.update(params)
If you want to do a single modification, you can simply type:
plt.rcParams.update({'mathtext.default': 'regular' })
In this respect, a trivial example would be as follows:
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(1, 10, 40)
y = x**2
fig = plt.figure()
ax = fig.add_subplot(111)
params = {'mathtext.default': 'regular' }
plt.rcParams.update(params)
ax.set_xlabel('$x_{my text}$')
ax.set_ylabel('$y_i$')
ax.plot(x, y)
ax.grid()
plt.show()
You can find more information on RcParams
in the matplotlib documentation.
1
votes
https://matplotlib.org/tutorials/text/mathtext.html
plt.figure()
plt.plot(x, y,'blue',label='$f(x)=e^{-x}$')
plt.plot(x,Mn3(x),'green',label='$M_{3}$')
plt.plot(x,Mn5(x),'red',label='$M_{5}$')
plt.plot(x,Mn7(x),'yellow',label='$M_{7}$')
plt.plot(x,Mn9(x),'pink',label='$M_{9}$')
plt.plot(x,Mn11(x),'black',label='$M_{11}$')
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.legend()
plt.show()