【发布时间】:2014-04-29 10:17:50
【问题描述】:
我从这个问题Matplotlib: Change math font size 中了解到如何更改matplotlib 中数学文本的默认大小。我要做的是:
from matplotlib import rcParams
rcParams['mathtext.default'] = 'regular'
这有效地使 LaTeX 字体与常规字体大小相同。
我不知道如何将其重置为默认行为,即:LaTeX 字体看起来比常规字体小一些。
我需要这个,因为我希望 LaTeX 字体仅在一个绘图上看起来与常规字体大小相同,而不是在我的图中使用 LaTex 数学格式的所有绘图上。
这是MWE 我如何创建我的图:
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重置为默认值?如果是这样,请使用plt.rcdefaults()。 -
或者只是把默认值放回去
it。 -
另外,如果你只想要特定的 mathtex 表达式的常规非斜体字体,只需
r'$\mathregular{your_expression_here}$' -
我尝试过
plt.rcdefaults()并设置rcParams['mathtext.default'] = 'it',但这两种方法都给了我同样的问题:它们要么影响整个数字,要么根本没有影响。让我提供一个更详细的示例来说明我的图是如何设置的,以便让你们更清楚地了解我的意思。 -
好的,我添加了
MWE。请注意,我只想覆盖中间图的默认行为,而不是其余部分。
标签: python fonts matplotlib latex