Matplotlib 自动化学式下标使用不用斜体文本

ede*_*esz 4 python plot matplotlib python-2.7

我试图用一个化学式自动标记 Python2.7 中 Matplotlib 图的 x 轴,该化学式以数字作为下标,没有斜体。

这是一个代码来显示这一点:

import numpy as np
import pylab as plt

x = np.array([1,2,3,4])
y = np.array([1,2,3,4])

chem_list = ['H2O','CO2','X7Z4']

for j, elem in enumerate(chem_list):
    plt.plot(x,y)
    plt.xlabel(chem_list[j])
    plt.legend(loc=1)
    plt.show()
Run Code Online (Sandbox Code Playgroud)

x 轴标签来自一个列表。列表中的每一项都是一个标签,我需要将其用作标签中的文本。

这种方法有效,但是,文本是斜体的,我需要避免使用斜体文本。产生斜体文本的另一篇文章:here

我的问题示例:

对于循环的第一次迭代,我想使用 H2O ax 作为 x 轴标签,但我不希望它被斜体化。

有没有办法在 Python 中做到这一点?

Jea*_*ien 5

根据文档 ( http://matplotlib.org/users/mathtext.html ),您可以使用\mathregular{...}mathtext 表达式中的常规文本。例如,对于你的情况,你会写:

chem_list = ['$\mathregular{H_2O}$','$\mathregular{CO_2}$','$\mathregular{X_7Z_4}$']
Run Code Online (Sandbox Code Playgroud)