我正在使用python脚本生成散点图。 现在,我想做一些小的修改,这是实际的代码
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams.update({'font.size': 22})
#Read datafile
Datafile='t_20_12.data'
name=[]
mean=[]
stdvar=[]
with open(Datafile,'r') as ins:
for line in ins:
line=line.strip('\n')
ind=line.index(' ')
name.append(line[0:ind])
while line[ind]==' ': ind+=1
line=line[ind:]
ind=line.index(' ')
mean.append(line[0:ind])
while line[ind]==' ': ind+=1
line=line[ind:]
stdvar.append(line)
original_name=['BASAL_INFEROSEPTAL','BASAL_INFERIOR','BASAL_INFEROLATERAL','BASAL_ANTEROLATERAL','BASAL_ANTERIOR','BASAL_ANTEROSEPTAL','MID_INFEROSEPTAL','MID_INFERIOR','MID_INFEROLATERAL','MID_ANTEROLATERAL','MID_ANTERIOR','MID_ANTEROSEPTAL','APICAL_INFERIOR','APICAL_LATERAL','APICAL_ANTERIOR','APICAL_SEPTAL']
short_name=['BasInfSep','BasInf','BasInfLat','BasAntLat','BasAnt','BasAntSep','MidInfSep','MidInf','MidInfLat','MidAntLat','MidAnt','MidAntSep','ApexInf','ApexLat','ApexAnt','ApexSep']
for i in range(len(name)):
origin=name[i]
ind=origin.index('_')
variable=origin[0:ind+1]
segment=origin[ind+1:]
modif_seg=short_name[original_name.index(segment)]
name[i]=variable+modif_seg
fig, ax = plt.subplots()
ax.scatter(mean,stdvar)
for i, txt in enumerate(name):
if (float(mean[i])>1e-4):
ax.annotate(txt, (mean[i], stdvar[i]))
plt.title('Sensitivity analysis: time')
plt.xlabel('Mean')
plt.ylabel('Standard variation')
plt.show()
这里是数据文件
a_BASAL_INFEROSEPTAL 0.000127547 0.000142708
b_BASAL_INFEROSEPTAL 5.47966e-05 0.000109593
e_BASAL_INFEROSEPTAL 0.000168496 0.000211819
XI_BASAL_INFEROSEPTAL 6.91457e-05 0.000138291
a_BASAL_INFERIOR 0.000133961 0.00013912
b_BASAL_INFERIOR 0 0
e_BASAL_INFERIOR 0 0
XI_BASAL_INFERIOR 0 0
a_BASAL_INFEROLATERAL 2.41402e-05 4.82804e-05
b_BASAL_INFEROLATERAL 0 0
e_BASAL_INFEROLATERAL 1.0847e-05 2.16941e-05
XI_BASAL_INFEROLATERAL 1.10378e-05 2.20757e-05
a_BASAL_ANTEROLATERAL 0.00014744 0.00029488
b_BASAL_ANTEROLATERAL 0 0
e_BASAL_ANTEROLATERAL 1.32931e-05 2.65863e-05
XI_BASAL_ANTEROLATERAL 0 0
a_BASAL_ANTERIOR 0.000115112 0.000230223
b_BASAL_ANTERIOR 1.0847e-05 2.16941e-05
e_BASAL_ANTERIOR 0 0
XI_BASAL_ANTERIOR 4.92837e-05 9.85674e-05
a_BASAL_ANTEROSEPTAL 0.000395227 0.000156225
b_BASAL_ANTEROSEPTAL 0.000110388 0.000194361
e_BASAL_ANTEROSEPTAL 6.46093e-05 0.000129219
XI_BASAL_ANTEROSEPTAL 6.68646e-05 0.000103741
a_MID_INFEROSEPTAL 9.81276e-06 1.96255e-05
b_MID_INFEROSEPTAL 0 0
e_MID_INFEROSEPTAL 0 0
XI_MID_INFEROSEPTAL 0.000181976 0.000149634
a_MID_INFERIOR 5.31215e-05 0.000106243
b_MID_INFERIOR 0 0
e_MID_INFERIOR 0 0
XI_MID_INFERIOR 6.07881e-05 0.000121576
a_MID_INFEROLATERAL 7.77117e-05 0.000130014
b_MID_INFEROLATERAL 0 0
e_MID_INFEROLATERAL 1.32931e-05 2.65863e-05
XI_MID_INFEROLATERAL 0.000121497 0.000242994
a_MID_ANTEROLATERAL 1.0847e-05 2.16941e-05
b_MID_ANTEROLATERAL 0.000115112 0.000230223
e_MID_ANTEROLATERAL 0 0
XI_MID_ANTEROLATERAL 0 0
a_MID_ANTERIOR 0.000136149 0.000142746
b_MID_ANTERIOR 0 0
e_MID_ANTERIOR 0 0
XI_MID_ANTERIOR 0 0
a_MID_ANTEROSEPTAL 0.000177225 0.00020393
b_MID_ANTEROSEPTAL 6.68646e-05 0.000133729
e_MID_ANTEROSEPTAL 0 0
XI_MID_ANTEROSEPTAL 6.29343e-05 0.000125869
a_APICAL_INFERIOR 0 0
b_APICAL_INFERIOR 6.92841e-05 0.000108435
e_APICAL_INFERIOR 0 0
XI_APICAL_INFERIOR 6.68646e-05 0.000133729
a_APICAL_LATERAL 0 0
b_APICAL_LATERAL 0 0
e_APICAL_LATERAL 0 0
XI_APICAL_LATERAL 0 0
a_APICAL_ANTERIOR 3.90355e-05 7.8071e-05
b_APICAL_ANTERIOR 0 0
e_APICAL_ANTERIOR 5.91207e-05 0.000118241
XI_APICAL_ANTERIOR 9.93506e-05 0.000198701
a_APICAL_SEPTAL 6.68646e-05 0.000133729
b_APICAL_SEPTAL 1.10378e-05 2.20757e-05
e_APICAL_SEPTAL 0 0
XI_APICAL_SEPTAL 9.93506e-05 0.000198701
它产生以下图片: Scatter points plot
这是我要修改的内容:
-更改标签的字体大小(注释功能中的txt),我试图查看可用于注释的选项,所以答案可能是对txt做些事情(我会研究一下) =>好的,看来matplotlib.rcParams.update({'font.size':22})会完成这项工作。解决并更新代码
-点和关联的标签太靠近,是否可以更改? =>我已经找到了一种使用
的方法 offset_x=str(float(mean[i])+0.00001)
ax.annotate(txt, (mean[i], stdvar[i]),(offset_x, stdvar[i]))
有没有更好/更清洁的方法?
-使给定标签位于与之关联的点的左侧或右侧,以使图形更具可读性。可以使用上一个问题中定义的offset_x来实现此目的,但是如果您有更好的主意...
当然,我会继续自行寻找答案,如果会的话,我会更新代码。 预先感谢您的帮助