Gab*_*iel 5 python fonts latex matplotlib
我从这个问题中学到了Matplotlib:更改数学字体大小如何更改数学文本的默认大小matplotlib
.我所做的是:
from matplotlib import rcParams
rcParams['mathtext.default'] = 'regular'
Run Code Online (Sandbox Code Playgroud)
这有效地使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()
Run Code Online (Sandbox Code Playgroud)
我认为这是因为该mathtext.default
设置是在绘制 Axes 对象时使用的,而不是在创建它时使用的。为了解决这个问题,我们需要在绘制 Axes 对象之前更改设置,下面是一个演示:
# 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
Run Code Online (Sandbox Code Playgroud)
这是输出:
归档时间: |
|
查看次数: |
4704 次 |
最近记录: |