I've learned from this question Matplotlib: Change math font size how to change the default size of math text in matplotlib
. What I do is:
from matplotlib import rcParams
rcParams['mathtext.default'] = 'regular'
which effectively makes the LaTeX font the same size as the regular font.
What I don't know is how to reset this back to the default behaviour, ie: the LaTeX font looking a bit smaller than the regular font.
I need this because I want the LaTeX font to look the same size as the regular font only on one plot, not on all plots in my figure that use LaTex math formatting.
Here's a MWE
of how I create my figure:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
# Generate random data.
x = np.random.randn(60)
y = np.random.randn(60)
fig = plt.figure(figsize=(5, 10)) # create the top-level container
gs = gridspec.GridSpec(6, 4) # create a GridSpec object
ax0 = plt.subplot(gs[0:2, 0:4])
plt.scatter(x, y, s=20, label='aaa$_{subaaa}$')
handles, labels = ax0.get_legend_handles_labels()
ax0.legend(handles, labels, loc='upper right', numpoints=1, fontsize=14)
plt.ylabel('A$_y$', fontsize=16)
plt.xlabel('A$_x$', fontsize=16)
ax1 = plt.subplot(gs[2:4, 0:4])
# I want equal sized LaTeX fonts only on this plot.
from matplotlib import rcParams
rcParams['mathtext.default'] = 'regular'
plt.scatter(x, y, s=20, label='bbb$_{subbbb}$')
handles, labels = ax1.get_legend_handles_labels()
ax1.legend(handles, labels, loc='upper right', numpoints=1, fontsize=14)
plt.ylabel('B$_y$', fontsize=16)
plt.xlabel('B$_x$', fontsize=16)
# If I set either option below the line that sets the LaTeX font as 'regular'
# is overruled in the entire plot.
#rcParams['mathtext.default'] = 'it'
#plt.rcdefaults()
ax2 = plt.subplot(gs[4:6, 0:4])
plt.scatter(x, y, s=20, label='ccc$_{subccc}$')
handles, labels = ax2.get_legend_handles_labels()
ax2.legend(handles, labels, loc='upper right', numpoints=1, fontsize=14)
plt.ylabel('C$_y$', fontsize=16)
plt.xlabel('C$_x$', fontsize=16)
fig.tight_layout()
out_png = 'test_fig.png'
plt.savefig(out_png, dpi=150)
plt.close()
rcParams
back to their defaults? If so, useplt.rcdefaults()
. – Joe Kingtonit
. – Aungr'$\mathregular{your_expression_here}$'
– Joe Kingtonplt.rcdefaults()
and also settingrcParams['mathtext.default'] = 'it'
but both approaches give me the same issue: they affect either the entire figure or nothing at all. Let me provide a more detailed example of how my figure is set to give you guys a more clear idea of what I mean. – GabrielMWE
. Note that I only want to override the default behaviour for the middle plot and not for the rest. – Gabriel