Home > Enterprise >  Set Latex greek letter in plotting script using matplotlib
Set Latex greek letter in plotting script using matplotlib

Time:01-22

I have a plotting script where I load subtitles (variable subplot_titles) from a JSON file :

example of JSON file :

"subplot_titles" : {

    "0" : "Model: $~w_{0},~w_{a}~$ - flat - optimistic - No $\\gamma$",
    "1" : "Model: $~w_{0},~w_{a}~$ - flat - optimistic - With $\\gamma$",
    "2" : "Model: $~w_{0},~w_{a}~$ - flat - semi-pessimistic - No $\\gamma$",
    "3" : "Model: $~w_{0},~w_{a}~$ - flat - semi-pessimistic - With $\\gamma$"
},

In my script, I load this file like this :

 for i, ax in enumerate(np.ravel(axes)):

    config = load_config('./config.json')

    df = parse_input(config['subplot_files'][i])
    df = format_dataframe(df, config)
    title = config['subplot_titles'][i]
    lgd = plot_barchart(df, ax, title)
    bbea.append(lgd)

But once the figure is generated, I have an uggly symbol "gamma", like this :

bad gamma

I would like to display a Latex gamma symbol.

I tried to add r' in the plotting script to get Latex support :

title = config[r'subplot_titles'][i]

But I get an error.

Could anyone see what can I do to ge this gamma greek symbol under Latex displaying ?

CodePudding user response:

You can change matplotlib rc settings to use LaTeX, for example by including the following code before you start plotting:

import matplotlib as mpl
mpl.rcParams['text.usetex'] = True

Alternatively, instead of using LaTeX you can just change the font that matplotlib is using to typeset mathematics:

mpl.rcParams['mathtext.fontset'] = 'cm'

'cm' is Computer Modern, the default LaTeX font, but there are also other possibilities.

  •  Tags:  
  • Related