Python matplotlib subscript font setting problem

created at 07-29-2021 views: 23

example

subscript font

The python code used (first step in your own python):

df_up= pd.read_excel(xlsxFilename)
df_up.index=['Q$_\mathrm{Oct}$','Q$_\mathrm{Nov}$','Q$_\mathrm{Dec}$','Q$_\mathrm{Jan}$']

This mathrm is the first step to make the subscript into the desired font

available fonts

If you don’t understand, you can copy the above code directly to your document.

global font settings

At this point, you will find that even after using the above, the picture that comes out still looks unsatisfactory, because it is just like the picture below

global font settings

If I want the above "sou" to also become "Times New Roman" font, what should I do?

plt.rcParams['mathtext.fontset'] = 'stix'# Most similar to Times new roman

Or (I didn't use this, but you can try)

import matplotlib.pyplot as plt
from matplotlib import rcParams
config = {
    "font.family":'serif',
    "font.size": 20,
    "mathtext.fontset":'stix',
    "font.serif": ['SimSun'],
}
rcParams.update(config)

Or:

plt.rcParams['font.family'] = "Times New Roman"
plt.rcParams["mathtext.fontset"] = "dejavuserif"
plt.rc('text', usetex=True )

debug

If you show up at this step:

RuntimeError: Failed to process string with tex because latex could not be found

you can

first step:

pip install latex

second step:
The main reason for this problem is the lack of latex, dvipng and ghostscript 
Just choose one of the 3 subordinates (anaconda)

conda install -c conda-forge jupyter_latex_envs
conda install -c conda-forge/label/cf201901 jupyter_latex_envs
conda install -c conda-forge/label/cf202003 jupyter_latex_envs

third step
After the above attempts, it doesn’t work, what should I do, download Protext and only install miktex

 install miktex

fourth step

Detect

test latex

created at:07-29-2021
edited at: 07-29-2021: