Matplotlib: Change math font size and then go back to default
I think this is because the mathtext.default
setting is used when the Axes object is drawn, not when it's created. To walk around the problem we need the change the setting just before the Axes object is drawn, here is a demo:
# your plot code here
def wrap_rcparams(f, params):
def _f(*args, **kw):
backup = {key:plt.rcParams[key] for key in params}
plt.rcParams.update(params)
f(*args, **kw)
plt.rcParams.update(backup)
return _f
plt.rcParams['mathtext.default'] = 'it'
ax1.draw = wrap_rcparams(ax1.draw, {"mathtext.default":'regular'})
# save the figure here
Here is the output:
Another solution is to change the rcParams settings to force matplotlib to use tex
for all the text (I'll not try to explain it because I only have a vague understanding of this setting). The idea is that by setting
mpl.rcParams['text.usetex']=True
You can pass string literals to any (or most of them?) text defining functions that will be passed to tex
, so you can use most of its (dark) magic. For this case, it is enough to use the \tiny
, \small
, \normalsize
, \large
, \Large
, \LARGE
, \huge
and \Huge
font size size commands
In your MWE
case it would be enough to change the second scatter line to
plt.scatter(x, y, s=20, label=r'bbb{\Huge$_{subbbb}$}')
to get a larger subscript font in the legend only in that case. All the other cases are handled correctly straight away