首页 > 解决方案 > Python 和 matplotlib:绘图图例中上标和下标字符的相等垂直间距(使用 Arial)

问题描述

考虑到我的上一篇文章,我需要更多关于 matplotlib 图形和字体的帮助。在ImportanceOfBeingErnest的友好帮助下,我能够在 Python 中创建绘图,但显然上标字符比使用 Arial 的下标字符(见图 1)具有更高的垂直空间或填充。我想继续使用 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.647058823529411964 E-02 9.435294117647058387E-01 2.000000000000000000 E+01 4.780876494023904300E-02 9.52191919191919191919760986E-012.00000000000000001777777877875757575758757578011.011.011.011.011.011.011.011.011.011.011.011.011011010101101010101110101011101010110111010111010110101101010110110110110110110110110110110110101011102ENEM800000000000000000e+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.996676820825256105 e-01 8.003323179174743895e-01 0.000000000000000000e+00 4.226958309964479188e-01 5.773041690035520812e-01441812564366632375e-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.773041690035520812 e-01441812564366632375e-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.773041690035520812 e-01859369061193462569e-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-01859369061193462569e-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-01003323179174743895e-01 0.000000000000000000e+00 4.226958309964479188e-01 5.773041690035520812e-01003323179174743895e-01 0.000000000000000000e+00 4.226958309964479188e-01 5.773041690035520812e-01

这将产生以下图: 图 1:示例图。

我希望间距(“A”和减号的间距在这个高度(使用 GIMP 专业操作): 图 2:结果。

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

标签: pythonmatplotliblatexsubscriptsuperscript

解决方案


我在 StackOverflow 上发现了一个类似的问题,它帮助我解决了我的问题。

正如Stop Harming Monica建议的mathtext.FontConstantBase那样,如果您使用 Arial,更新下面提到的参数会有所帮助。有关课程的更多信息,请参阅此链接。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

请注意,上标字符受输出图中下标参数位置的影响。我发现没有mathtext.FontConstantsBase.sup2-parameter 来解决这个问题。

这是我使用的修改后的 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

这产生了这个数字


推荐阅读