当同时使用 super 和 subscript 时,Matplotlib 2.0 下标超出基线
Matplotlib 2.0 subscript outside of baseline when super and subscript are both used
在 matplotlib 2.0 中,当我在同一个字符上同时使用下标和上标时,我遇到了奇怪的行为。当它们合并时,下标完全下降到基线以下。这在 MPL 1.5 中没有发生。这是一个完整的例子:
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc("font", family="Times New Roman",weight='normal')
plt.rcParams.update({'mathtext.default': 'regular' })
plt.plot(1,1, label='$A_x^{b}$')
plt.plot(2,2,label='$A_x$')
plt.plot(3,3,label='$A^b$')
plt.plot(4,4,label='$A_x^{*}$')
plt.plot(5,5,label='$A^*$')
plt.legend(fontsize='xx-large')
plt.show()
我已经拍摄了这张图并放大了图例并绘制了一些水平线以显示相对的上标和下标位置。
我在 mathtext.py 文件中的 class FontConstantBase 下找到了这些参数:
# Percentage of x-height of additional horiz. space after sub/superscripts
script_space = 0.05
# Percentage of x-height that sub/superscripts drop below the baseline
subdrop = 0.4
# Percentage of x-height that superscripts are raised from the baseline
sup1 = 0.7
# Percentage of x-height that subscripts drop below the baseline
sub1 = 0.3
# Percentage of x-height that subscripts drop below the baseline when a
# superscript is present
sub2 = 0.5
# Percentage of x-height that sub/supercripts are offset relative to the
# nucleus edge for non-slanted nuclei
delta = 0.025
# Additional percentage of last character height above 2/3 of the
# x-height that supercripts are offset relative to the subscript
# for slanted nuclei
delta_slanted = 0.2
# Percentage of x-height that supercripts and subscripts are offset for
# integrals
delta_integral = 0.1
之前的版本有sub2吗?从 0.3 到 0.5 真的可以像我看到的那样将它完全降低到基线以下吗?我想要同步的上标和下标不完全在基线之外,除了修改 mathtext.py 本身之外我看不到任何其他方法。此外,当在上标中包含星号时,它似乎也高于 mpl 2.0 的预期。有没有办法在不更改数学文本的情况下将其降低一点?谢谢。
似乎没有 API 可以更改此设置,但您可以 monkey-patch 适当的 class 而不是编辑 mathtext.py
.
使用默认的 mathtext 字体,如果有上标,则下标的位置会发生变化(不完全在基线下方,但您可以看到效果):
def test_plot():
plt.figure()
plt.plot(1, 1, label="$A_x^b$")
plt.plot(2, 2, label="$A^b_x$")
plt.plot(3, 3, label="$A_x$")
plt.plot(4, 4, label="$A_x^*$")
plt.plot(4, 4, label="$A^*_x$")
plt.plot(5, 5, label="$A^*$")
plt.legend(fontsize="xx-large")
# default mathtext font in matplotlib 2.0.0 is 'dejavusans'
# set explicitly for reproducibility
plt.rcParams["mathtext.fontset"] = "dejavusans"
test_plot()
Monkey-patching mathtext.DejaVuSansFontConstants
你可以让效果消失:
import matplotlib.mathtext as mathtext
mathtext.DejaVuSansFontConstants.sub2 = 0.3 # default 0.5
test_plot()
(对于较新版本的 matplotlib,如 3.4.2,此 class 似乎已移至 _mathtext
子模块。您可能需要执行以下操作:)
# Later versions of matplotlib (e.g., 3.4.2)
from matplotlib.mathtext import _mathtext as mathtext
mathtext.FontConstantsBase.sup1 = 0.5
我看不出星号有任何问题。
我没有安装 Times New Roman,所以我无法测试您的确切用例,但您可能需要修补 FontConstantsBase
而不是 DejaVuSansFontConstants
。使用 Liberation Serif 对我有用。
在 matplotlib 2.0 中,当我在同一个字符上同时使用下标和上标时,我遇到了奇怪的行为。当它们合并时,下标完全下降到基线以下。这在 MPL 1.5 中没有发生。这是一个完整的例子:
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc("font", family="Times New Roman",weight='normal')
plt.rcParams.update({'mathtext.default': 'regular' })
plt.plot(1,1, label='$A_x^{b}$')
plt.plot(2,2,label='$A_x$')
plt.plot(3,3,label='$A^b$')
plt.plot(4,4,label='$A_x^{*}$')
plt.plot(5,5,label='$A^*$')
plt.legend(fontsize='xx-large')
plt.show()
我已经拍摄了这张图并放大了图例并绘制了一些水平线以显示相对的上标和下标位置。
我在 mathtext.py 文件中的 class FontConstantBase 下找到了这些参数:
# Percentage of x-height of additional horiz. space after sub/superscripts
script_space = 0.05
# Percentage of x-height that sub/superscripts drop below the baseline
subdrop = 0.4
# Percentage of x-height that superscripts are raised from the baseline
sup1 = 0.7
# Percentage of x-height that subscripts drop below the baseline
sub1 = 0.3
# Percentage of x-height that subscripts drop below the baseline when a
# superscript is present
sub2 = 0.5
# Percentage of x-height that sub/supercripts are offset relative to the
# nucleus edge for non-slanted nuclei
delta = 0.025
# Additional percentage of last character height above 2/3 of the
# x-height that supercripts are offset relative to the subscript
# for slanted nuclei
delta_slanted = 0.2
# Percentage of x-height that supercripts and subscripts are offset for
# integrals
delta_integral = 0.1
之前的版本有sub2吗?从 0.3 到 0.5 真的可以像我看到的那样将它完全降低到基线以下吗?我想要同步的上标和下标不完全在基线之外,除了修改 mathtext.py 本身之外我看不到任何其他方法。此外,当在上标中包含星号时,它似乎也高于 mpl 2.0 的预期。有没有办法在不更改数学文本的情况下将其降低一点?谢谢。
似乎没有 API 可以更改此设置,但您可以 monkey-patch 适当的 class 而不是编辑 mathtext.py
.
使用默认的 mathtext 字体,如果有上标,则下标的位置会发生变化(不完全在基线下方,但您可以看到效果):
def test_plot():
plt.figure()
plt.plot(1, 1, label="$A_x^b$")
plt.plot(2, 2, label="$A^b_x$")
plt.plot(3, 3, label="$A_x$")
plt.plot(4, 4, label="$A_x^*$")
plt.plot(4, 4, label="$A^*_x$")
plt.plot(5, 5, label="$A^*$")
plt.legend(fontsize="xx-large")
# default mathtext font in matplotlib 2.0.0 is 'dejavusans'
# set explicitly for reproducibility
plt.rcParams["mathtext.fontset"] = "dejavusans"
test_plot()
Monkey-patching mathtext.DejaVuSansFontConstants
你可以让效果消失:
import matplotlib.mathtext as mathtext
mathtext.DejaVuSansFontConstants.sub2 = 0.3 # default 0.5
test_plot()
(对于较新版本的 matplotlib,如 3.4.2,此 class 似乎已移至 _mathtext
子模块。您可能需要执行以下操作:)
# Later versions of matplotlib (e.g., 3.4.2)
from matplotlib.mathtext import _mathtext as mathtext
mathtext.FontConstantsBase.sup1 = 0.5
我看不出星号有任何问题。
我没有安装 Times New Roman,所以我无法测试您的确切用例,但您可能需要修补 FontConstantsBase
而不是 DejaVuSansFontConstants
。使用 Liberation Serif 对我有用。