如何在matplotlib中生成无衬线上标或下标文本?

2024-04-26 18:13:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在matplotlib图形的轴标签中使用下标。使用LaTeX,我会将其设置为$N_i$,这将为我提供斜体衬线字体。我知道我可以用\mathrm得到非斜体的mathfont。但是我希望得到默认matplotlib sans serif字体的文本,以便它与图中的其余文本匹配。有没有一种方法可以在不使用乳胶的情况下下标文本?


Tags: 方法文本图形matplotlib字体标签latex乳胶
2条回答

使用\mathregular来使用mathtext之外的常规文本使用的字体:

$\mathregular{N_i}$

查看here了解更多信息。

您可以通过定制rcParams来完成。如果要自定义多个元素,可以将它们存储为dict,并更新“rcParams”:

params = {'mathtext.default': 'regular' }          
plt.rcParams.update(params)

如果要进行单个修改,只需键入:

plt.rcParams.update({'mathtext.default':  'regular' })

在这方面,一个简单的例子如下:

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()

enter image description here

您可以在matplotlib documentation中找到有关RcParams的更多信息。

相关问题 更多 >