当同时使用上标和下标时,基线之外的 Matplotlib 2.0 下标 [英] Matplotlib 2.0 subscript outside of baseline when super and subscript are both used

查看:38
本文介绍了当同时使用上标和下标时,基线之外的 Matplotlib 2.0 下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 matplotlib 2.0 时,我在同一个字符上同时使用下标和上标时出现奇怪的行为.当它们组合在一起时,下标完全下降到基线以下.MPL 1.5并没有发生这种情况.这是一个完整的示例:

With matplotlib 2.0 I have been having strange behavior when I use both subscript and superscript on the same character. When they are combined, the subscript goes down entirely below the baseline. This didn't happen with MPL 1.5. Here is a complete example:

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()

我已经拍摄了这个图并放大了图例并绘制了一些水平线来显示相对的上标和下标位置.

I've taken this plot and zoomed in on the legend and drawn some horizontal lines to show the relative super and subscript positions.

我在 mathtext.py 文件中找到了类 FontConstantBase 下的这些参数:

I found in the mathtext.py file these parameters under the 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 的预期.有没有办法在不更改mathtext的情况下将其降低一点?谢谢.

Did sub2 exist in previous versions? Could going from 0.3 to 0.5 really drop it completely below the baseline like I'm seeing? I'd like to have simultaneous superscripts and subscripts that aren't completely outside the baseline and I don't see any other way besides modifying mathtext.py itself. Also, it appears that when including an asterisk in the superscript, it also goes higher than anticipated with mpl 2.0. Is there a way to lower it down just a bit without changing mathtext? Thanks.

推荐答案

似乎没有API可以对此进行更改,但是您可以猴子修补适当的类,而不用编辑 mathtext.py .

There seems to be no API to change this but you can monkey-patch the apropiate class instead of editing mathtext.py.

使用默认的mathtext字体,如果有上标(不是完全位于基线之下,但可以看到效果),则下标的位置会发生变化:

Using the default mathtext font the position of the subscript changes if there is a superscript (not totally under the baseline but you can see the effect):

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 你可以让效果消失:

Monkey-patching mathtext.DejaVuSansFontConstants you can make the effect disappear:

import matplotlib.mathtext as mathtext
mathtext.DejaVuSansFontConstants.sub2 = 0.3  # default 0.5
test_plot()

我看不到星号有任何问题.

I can't see any issue with the asterisk.

我没有安装Times New Roman,因此我无法测试您的确切用例,但可能您需要修补 FontConstantsBase 而不是 DejaVuSansFontConstants .使用 Liberation Serif 对我有用.

I don't have Times New Roman installed so I can't test your exact use case but probably you need to patch FontConstantsBase instead of DejaVuSansFontConstants. It worked for me using Liberation Serif.

这篇关于当同时使用上标和下标时,基线之外的 Matplotlib 2.0 下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆