gpt4 book ai didi

python - 我如何在 matplotlib 中使用下标文本进行注释?

转载 作者:行者123 更新时间:2023-11-28 22:01:15 24 4
gpt4 key购买 nike

我正在尝试使用 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”以下标结尾。

最佳答案

我们可以在 'RbB2' 中显示带有下标的 2,使用 TeX 表示法:$\tt{RbB_ {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()

关于python - 我如何在 matplotlib 中使用下标文本进行注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13268587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com