這是可能的,將您的乳膠\newcommand
放入text.latex.preamble
的plt.rcParams
。在這裏,我使用answer you linked to的羅馬數字命令。爲了幫助轉換LaTeX字符,我們可以使用原始字符串使事情變得更簡單(在字符串前加上r
字符)。
import matplotlib.pyplot as plt
# Turn on LaTeX formatting for text
plt.rcParams['text.usetex']=True
# Place the command in the text.latex.preamble using rcParams
plt.rcParams['text.latex.preamble']=r'\makeatletter \newcommand*{\rom}[1]{\expandafter\@slowromancap\romannumeral #[email protected]} \makeatother'
fig,ax = plt.subplots(1)
# Lets try it out. Need to use a 'raw' string to escape
# the LaTeX command properly (preface string with r)
ax.text(0.2,0.2,r'\rom{28}')
# And to use a variable as the roman numeral, you need
# to use double braces inside the LaTeX braces:
for i in range(1,10):
ax.text(0.5,float(i)/10.,r'\rom{{{}}}'.format(i))
plt.show()

來源
2015-12-09 11:53:33
tom
謝謝您的回答。 但是,您的示例對我無效。當我複製你的工作示例時,我獲得了相同的數字,但不是羅馬數字,\ rom {1},\ rom {2}等等.. – user1991
啊,看起來我在我的'matplotlibrc'中有一些東西別。嘗試在其他'rcParams'行之前添加'plt.rcParams ['text.usetex'] = True'(請參閱上面的我的編輯) – tom