italic symbols in matplotlib?

本小妞迷上赌 提交于 2021-02-08 13:06:13

问题


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


回答1:


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()



回答2:


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:




回答3:


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()


来源:https://stackoverflow.com/questions/32470137/italic-symbols-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!