14

I'm trying to get the gamma symbol to be italicised in a plot label but can't figure out how to do it?

I thought this should work (but doesn't)

plt.xlabel(r'$\mathit{\Gamma}$$^{*}$')

I should add I am using the Helvetica font, so don't want to switch into tex mode, e.g. this doesn't work for me:

import matplotlib
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = "sans-serif"
plt.rcParams['font.sans-serif'] = "Helvetica"
plt.rcParams['text.usetex'] = True
plt.plot(range(5), range(5))
plt.title('$\Gamma + \mathit{\Gamma}$', fontsize=40)
plt.show()

thanks,

Martin

user1516252
  • 237
  • 2
  • 3
  • 6

3 Answers3

21

Write the text between '$$' that forces the text to be interpreted.

import matplotlib.pyplot as plt

plt.plot(range(5), range(5))
plt.title('$\it{text you want to show in italics}$')
plt.show()
  • 5
    Hi Homayoun. I have a problem, when I'm using your method. My text goes italic but font changes, and isn't Times New Roman anymore. Do you know how to fix that? – Cro Simpson2.0 Jan 01 '20 at 19:49
  • 2
    Hi @CroSimpson2.0, I think your problem may be solved by setting `plt.rcParams['text.usetex'] = False` (which is the default). Matplotlib then uses [it's own TeX-like parsing](https://matplotlib.org/stable/tutorials/text/mathtext.html) and should not switch fonts. – Arne L. Feb 23 '22 at 10:21
5

You should set usetex = True. See here.

import matplotlib
import matplotlib.pyplot as plt

matplotlib.rc('text', usetex = True)

plt.plot(range(5), range(5))
plt.title('$\Gamma + \mathit{\Gamma}$', fontsize=40)
plt.show()

Output:

enter image description here

Sait
  • 19,045
  • 18
  • 72
  • 99
  • 3
    I don't want to switch into tex mode as I'm using the Helvetica font. I've edited my question above (thanks though). – user1516252 Sep 09 '15 at 03:41
  • Nothing really prevents you from using Helvetica in TeX. Add `\usepackage[scaled]{helvet}`, `\renewcommand{\familydefault}{\sfdefault}` and `\usepackage[T1]{fontenc}` to the preamble (`pgf.preamble` field in `matplotlib`'s `rcParams`). – fjarri Sep 09 '15 at 04:20
-1

One solution I've found:

import matplotlib
import matplotlib.pyplot as plt

plt.rcParams['font.family'] = "sans-serif"
plt.rcParams['font.sans-serif'] = "Helvetica"
plt.rcParams['text.usetex'] = False
plt.plot(range(5), range(5))
plt.title(u'${\u0413}$', fontsize=20)
plt.show()
user1516252
  • 237
  • 2
  • 3
  • 6