2012-11-07 57 views
2

我試圖用matplotlib繪製一些數據,我需要一些註釋,如數學/化學公式。 這裏是我的一些代碼。我如何註釋matplotlib中的腳本文字?

#!/usr/bin/python2 
import numpy as np 
import matplotlib.pyplot as pytl 
from matplotlib import rc 
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) 
rc('text', usetex=True) 
recdt = np.dtype([('compound',str,4),('H_v','f4'),('B_o','f4')]); 
gat = np.loadtxt('tra',dtype=object, usecols=(0,1,2),unpack=True); 
gct,ght,gbt=[],[],[] 
for c,h,b in np.transpose(gat): 
    gct=np.append(gct,c) 
    ght=np.append(ght,h) 
    gbt=np.append(gbt,b) 
ght= ght.astype(np.float) 
gbt= gbt.astype(np.float) 
hard = pytl 
four = hard ##### 
four.scatter(gbt,ght) 
hard.title('physical stuff') 
hard.xlabel('physical prop 1') 
hard.ylabel('physical prop2 ') 
for l,x1,y2 in zip (gct,gbt,ght): 
    hard.annotate(l,xy=(x1,y2),xytext=(-24,12),textcoords = 'offset points', arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'),rotation=0) 
hard.ylim([0,10]) 
hard.savefig('hardcomp.png') 
hard.show() 

,這裏是一些測試數據

ZrC 6.8 1 
NbC 8 2 
NbN 7 13 
RuB2 30 5 
BP 3 1 
AlP 9.4 3 
InSb 2.2 47 
C 6 4 

的數據分三路一個文本其他兩個是數字。 我想'RbB2'中的'2'結尾爲下標。

+0

請解決您的縮進的示例代碼。 –

+0

@DavidZwicker:固定 – mike

回答

5

我們可以使用TeX表示法$\tt{RbB_{2}}$'RbB2'的下標顯示2。在代碼中,你只需要修改c

import re 
for c,h,b in np.transpose(gat): 
    c = r'$\tt{{{c}}}$'.format(c = re.sub(r'(\d+)',r'_{\1}', c)) 

這將產生

enter image description here


import re 
import numpy as np 
import matplotlib.pyplot as pytl 
from matplotlib import rc 

rc('font', **{'family':'sans-serif', 'sans-serif':['Helvetica']}) 
rc('text', usetex = True) 
recdt = np.dtype([('compound', str, 4), ('H_v', 'f4'), ('B_o', 'f4')]); 
gat = np.loadtxt('tra', dtype = object, usecols = (0, 1, 2), unpack = True); 
gct, ght, gbt = [], [], [] 
for c, h, b in np.transpose(gat): 
    c = r'$\tt{{{c}}}$'.format(c = re.sub(r'(\d+)', r'_{\1}', c)) 
    gct = np.append(gct, c) 
    ght = np.append(ght, h) 
    gbt = np.append(gbt, b) 
ght = ght.astype(np.float) 
gbt = gbt.astype(np.float) 
hard = pytl 
four = hard ##### 
four.scatter(gbt, ght) 
hard.title('physical stuff') 
hard.xlabel('physical prop 1') 
hard.ylabel('physical prop2 ') 
for l, x1, y2 in zip (gct, gbt, ght): 
    print(l, x1, y2) 
    hard.annotate(
     l, xy = (x1, y2), xytext = (-24, 12), textcoords = 'offset points', 
     arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'), 
     rotation = 0) 
hard.ylim([0, 10]) 
hard.savefig('hardcomp.png') 
hard.show() 
+0

完美。 +1,那確切地說是 – mike

+0

@tcaswell完成了,我的不好。 – mike

+0

@mike歡迎來到SO – tacaswell