2015-12-09 282 views
1

在python情節中,我想用羅馬數字標註註釋。即「I」,「II」,「III」和「IV」。Pyplot註釋:羅馬數字

現在,簡單的做法是簡單地使用字符串作爲「I」,「II」等,但我希望它們被專門排版爲羅馬數字(所以,包括頂部的水平條和例如下面的例子:

困難主要在於使用LateX命令,就像我爲其他符號(例如\ alpha)所做的那樣,似乎不可能,因爲如果有人想在LateX中使用羅馬數字,通常的做法是定義a new command,我不知道如何把該Python環境中。

任何想法?

回答

1

這是可能的,將您的乳膠\newcommand放入text.latex.preambleplt.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() 

enter image description here

+0

謝謝您的回答。 但是,您的示例對我無效。當我複製你的工作示例時,我獲得了相同的數字,但不是羅馬數字,\ rom {1},\ rom {2}等等.. – user1991

+0

啊,看起來我在我的'matplotlibrc'中有一些東西別。嘗試在其他'rcParams'行之前添加'plt.rcParams ['text.usetex'] = True'(請參閱上面的我的編輯) – tom