With my last post in mind, I need some more help with matplotlib graphics and fonts. With the friendly help of ImportanceOfBeingErnest, I was able to create plots in Python, but superscript characters have a higher vertical space or padding than subscript characters (see figure 1) using Arial, apparently. I would like to continue using Arial, but super- and subscript characters to be aligned normally (see figure 2). My minimal working example is as follows:
# 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()
The data used is:
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
This yields the following plot: Figure 1: Example plot.
I would like the spacing (of the 'A' and the minus sign to be in this height (professionally manipulated using GIMP): Figure 2: Outcome.
Note that the data used for the figures differs from the data used in the MWE.
I found a similar question on StackOverflow, which helped me solve my problem.
As Stop Harming Monica suggested, updating the mathtext.FontConstantBase
parameters mentioned below helps, if you are using Arial. See this link for more information on mathtext
classes.
The following parameter handles all superscript characters. It accepts a floating point number like 0.5
:
mathtext.FontConstantsBase.sup1 = 0.5
The following two parameters handle subscript characters. If I'm correct, sub1
handles subscript characters, which are in the same position as superscript characters as in X^A_B
. On the other hand, sub2
handles unpaired subscript characters like X_B
.
mathtext.FontConstantsBase.sub1 = 0.4
mathtext.FontConstantsBase.sub2 = 0.4
Note that the superscript character is influenced by the position of the subscript parameter in the output figure. I found there to be no mathtext.FontConstantsBase.sup2
-parameter to tackle this issue.
This is the modified MWE I used:
# 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
Which produces this figure.