Python 如何在 matplotlib 中制作 sans serif 上标或下标文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27698377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make sans serif superscript or subscript text in matplotlib?
提问by frixhax
I want to use a subscript in an axis label in a matplotlib figure. Using LaTeX I would set it as $N_i$
, which gives me the italic serif font. I know I can get non-italic mathfont with \mathrm
. But I would like to get the text in the default matplotlib sans-serif font so it matches the rest of the text in the figure. Is there a way to subscript text without using latex?
我想在 matplotlib 图中的轴标签中使用下标。使用 LaTeX 我会将它设置为$N_i$
,这给了我斜体衬线字体。我知道我可以使用\mathrm
. 但我想以默认的 matplotlib sans-serif 字体获取文本,以便它与图中的其余文本相匹配。有没有办法在不使用乳胶的情况下为文本添加下标?
采纳答案by skytux
回答by T-800
You can do it by customizing rcParams
. If you have multiple elements to customize, you can store them as a dict
and the update the `rcParams':
您可以通过自定义来做到这一点rcParams
。如果您有多个要自定义的元素,您可以将它们存储为 adict
并更新 `rcParams':
params = {'mathtext.default': 'regular' }
plt.rcParams.update(params)
If you want to do a single modification, you can simply type:
如果你想做一个单一的修改,你可以简单地输入:
plt.rcParams.update({'mathtext.default': 'regular' })
In this respect, a trivial example would be as follows:
在这方面,一个简单的例子如下:
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()
You can find more information on RcParams
in the matplotlib documentation.
您可以RcParams
在matplotlib 文档中找到更多信息。