Skip to content Skip to sidebar Skip to footer

Matplotlib 2.0 Subscript Outside Of Baseline When Super And Subscript Are Both Used

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 be

Solution 1:

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

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

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

enter image description here

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

enter image description here

(For more recent versions of matplotlib, like 3.4.2, this class appears to have been moved to a _mathtext submodule. You may need to do something like the following:)

# Later versions of matplotlib (e.g., 3.4.2)from matplotlib.mathtext import _mathtext as mathtext

mathtext.FontConstantsBase.sup1 = 0.5

I can't see any issue with the asterisk.

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.

Post a Comment for "Matplotlib 2.0 Subscript Outside Of Baseline When Super And Subscript Are Both Used"