Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get non-italic greek characters in a title/label (matplotlib)

I am trying to get non-italic greek characters into my matplotlib titles and labels, for example μ. I have so far written greek characters as follows:

plt.title('$\mu$)

This would print an italic mu. Now hoping to get it to be non-italic, I have tried to do:

plt.title('$\mathrm{\mu}$')

Sadly this does not change it at all, even though using \mathrm does work for normal characters. For example plt.title('$\mathrm{test}$') correctly prints 'test' in a non-italic font.

Why is this behaviour different for greek characters, and how do I fix this? I have googled and found more questions on this problem, but I couldn't get a clear answer on why \mathrm does not work and how I can solve this.

Thanks in advance!

like image 430
Aart Avatar asked Jan 26 '26 00:01

Aart


1 Answers

This is more of a TeX font issue rather than a Python issue. The best solution for you is to use change the latex rc config for matplotlib like below

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['mathtext.default'] = 'regular'

Or in case you need italics in some parts just add \it before a text or a symbol.

like image 58
Nishant Patel Avatar answered Jan 28 '26 14:01

Nishant Patel