Python Pyplot注释:罗马数字

Python Pyplot注释:罗马数字,python,matplotlib,annotations,latex,Python,Matplotlib,Annotations,Latex,在python绘图中,我想用罗马数字添加注释。即“I”、“II”、“III”和“IV” 现在,最简单的方法是简单地使用字符串作为“I”、“II”等,但我希望它们被专门排版为罗马数字(例如,包括III顶部和下方的水平条) 困难主要在于,使用LateX命令,就像我对其他符号(例如.....alpha)所做的那样,似乎是不可能的,因为如果想在LateX中使用罗马数字,通常会定义,我不知道如何将其合并到python环境中 有什么想法吗?这是可能的,方法是将LaTeX\newcommand放在中的text

在python绘图中,我想用罗马数字添加注释。即“I”、“II”、“III”和“IV”

现在,最简单的方法是简单地使用字符串作为“I”、“II”等,但我希望它们被专门排版为罗马数字(例如,包括III顶部和下方的水平条)

困难主要在于,使用LateX命令,就像我对其他符号(例如.....alpha)所做的那样,似乎是不可能的,因为如果想在LateX中使用罗马数字,通常会定义,我不知道如何将其合并到python环境中


有什么想法吗?

这是可能的,方法是将LaTeX
\newcommand
放在中的
text.LaTeX.preamble
中。在这里,我使用中的罗马数字命令。为了帮助转义LaTeX字符,我们可以使用原始字符串使事情更简单(在字符串前面加上
r
字符)


谢谢你的回答。但是,你的例子对我来说不起作用。当我复制你的工作例子时,我得到的是相同的数字,但不是罗马数字、\rom{1}、\rom{2}等等..啊,看起来我在我的
matplotlibrc
中有一些你没有的东西。尝试添加
plt.rcParams['text.usetex']=True
在另一个
rcParams
行之前(请参见上面的编辑)
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 #1@} \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()