Python 和 matplotlib:图例中上标和下标字符的垂直间距相等(使用 Arial)

Python and matplotlib: Equal vertical spacing of superscript and subscript characters (using Arial) in a plot's legend

在我的最后一个 in mind, I need some more help with matplotlib graphics and fonts. With the friendly help of ImportanceOfBeingErnest 中,我能够在 Python 中创建绘图,但是上标字符具有比下标字符更高的垂直 space 或填充(见图 1)使用显然,Arial。我想继续使用 Arial,但要正常对齐上标和下标字符(见图 2)。我的最小工作示例如下:

# MWE - python 3.6.4 / 3.7.2

import numpy as np

import matplotlib as mpl
from matplotlib import rcParams
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot, show

# -------------------------------------------------------------------

rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['Arial']
rcParams['font.size'] = 15

# labels in TeX-format are given in a different file in the original program

label_list=["[MX_4(^AY)_4]^-", "[MX(^AY)_4]^-"]

output_array = np.genfromtxt("oa.txt", dtype=float, delimiter=" ")

for ion in range(len(label_list)):
    plt.plot(output_array[:,0], output_array[:,ion], marker="s",label=r"$\mathregular{%s}$" % (label_list[ion-1]))
    plt.legend()

plt.show()

使用的数据是:

0.000000000000000000e+00 4.127290260366441865e-01 5.872709739633558135e-01 1.000000000000000000e+00 2.891558566042558565e-01 7.108441433957440880e-01 2.000000000000000000e+00 1.979585968947671082e-01 8.020414031052328641e-01 3.000000000000000000e+00 1.238903898108838220e-01 8.761096101891161503e-01 4.000000000000000000e+00 6.903086085544125894e-02 9.309691391445586994e-01 5.000000000000000000e+00 3.809897879025923167e-02 9.619010212097407475e-01 6.000000000000000000e+00 2.185727788279773209e-02 9.781427221172023234e-01 7.000000000000000000e+00 1.441899915182357980e-02 9.855810008481764584e-01 8.000000000000000000e+00 9.900990099009901110e-03 9.900990099009900902e-01 9.000000000000000000e+00 1.037181996086105652e-02 9.896281800391389938e-01 1.000000000000000000e+01 1.068883610451306330e-02 9.893111638954869003e-01 1.100000000000000000e+01 4.562043795620437589e-03 9.954379562043795815e-01 1.200000000000000000e+01 1.573033707865168634e-02 9.842696629213483206e-01 1.300000000000000000e+01 1.270588235294117622e-02 9.872941176470588776e-01 1.400000000000000000e+01 1.210121012101210078e-02 9.878987898789879374e-01 1.500000000000000000e+01 8.961911874533233513e-03 9.910380881254667873e-01 1.600000000000000000e+01 2.255639097744360777e-02 9.774436090225563367e-01 1.700000000000000000e+01 2.549575070821529649e-02 9.745042492917846966e-01 1.800000000000000000e+01 2.564102564102564014e-02 9.743589743589743390e-01 1.900000000000000000e+01 5.647058823529411964e-02 9.435294117647058387e-01 2.000000000000000000e+01 4.780876494023904300e-02 9.521912350597609986e-01 2.000000000000000000e+01 1.010452961672473893e-01 8.989547038327526662e-01 1.800000000000000000e+01 4.583333333333333010e-02 9.541666666666667185e-01 1.600000000000000000e+01 1.441812564366632375e-02 9.855818743563337092e-01 1.400000000000000000e+01 2.482678983833718281e-02 9.751732101616628068e-01 1.200000000000000000e+01 1.406309388065374311e-02 9.859369061193462569e-01 1.000000000000000000e+01 5.292405398253506588e-03 9.947075946017465142e-01 8.000000000000000000e+00 9.794507393892835923e-03 9.902054926061071294e-01 6.000000000000000000e+00 2.557103864387300779e-02 9.744289613561269991e-01 4.000000000000000000e+00 7.076957695769577061e-02 9.292304230423041878e-01 2.000000000000000000e+00 1.996676820825256105e-01 8.003323179174743895e-01 0.000000000000000000e+00 4.226958309964479188e-01 5.773041690035520812e-01

这会产生以下情节: Figure 1: Example plot.

我希望 'A' 和减号的间距在此高度(使用 GIMP 进行专业操作): Figure 2: Outcome.

请注意,图中使用的数据与 MWE 中使用的数据不同。

我在 Whosebug 上找到了类似的 ,它帮助我解决了我的问题。

As Stop Harming Monica suggested, updating the mathtext.FontConstantBase parameters mentioned below helps, if you are using Arial. See this link 有关 mathtext 类.

的更多信息

以下参数处理所有上标字符。它接受一个浮点数,如 0.5:

mathtext.FontConstantsBase.sup1 = 0.5

以下两个参数处理下标字符。如果我是正确的,sub1 处理下标字符,这些字符与 X^A_B 中的上标字符处于相同的位置。另一方面,sub2 处理不成对的下标字符,如 X_B

mathtext.FontConstantsBase.sub1 = 0.4
mathtext.FontConstantsBase.sub2 = 0.4

注意上标字符受output figure中下标参数位置的影响。我发现没有 mathtext.FontConstantsBase.sup2 参数来解决这个问题。

这是我使用的修改后的 MWE:

# MWE update - python 3.6.4 // 3.7.2

import numpy as np

import matplotlib as mpl
import matplotlib.mathtext as mathtext
from matplotlib import rcParams
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot, show

# -------------------------------------------------------------------

rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['Arial']
rcParams['font.size'] = 25
mathtext.FontConstantsBase.sup1 = 0.
mathtext.FontConstantsBase.sub1 = 1.
mathtext.FontConstantsBase.sub2 = 0.5

# labels in TeX-format are given in a different file in the original program

label_list=["[MX^A_B(^AY)_4]^-", "[MX(^AY)_4]^-"]

output_array = np.genfromtxt("oa.txt", dtype=float, delimiter=" ")

for ion in range(len(label_list)):
    plt.plot(output_array[:,0], output_array[:,ion], marker="s", label=r"$\mathregular{%s}$" % (label_list[ion-1]))
    plt.legend()

plt.show

产生 this figure.