Python代码:VT值分布图绘制及Y轴对数化
Python代码:VT值分布图绘制及Y轴对数化
本文提供Python代码,用于绘制VT值分布图,并解释如何使用plt.yscale()函数将Y轴排列方式改为对数。
import numpy as np
import matplotlib.pyplot as plt
import os
import math
import time
### step1: 按照每一片画出不同目录(时间节点)的vt 值的分布
path='.\readvt\log\'
#path='.\vt-32Mb-skyworth\'
path1='.\readvt\results\'
dir=[]
abnorm=[]
vrd=6.0
files=os.listdir(path)
for i in files:
if os.path.isdir(path+i):
dir.append(i)
print(dir)
slotn=['1']
#files=['#1 2.log', '#3 4.log', '#5 6.log', '#7 8.log', '#9 10.log',]#,'3.log','4.log','5.log','6.log','7.log','8.log','9.log','10.log','11.log','12.log','13.log',
# '14.log','15.log','16.log','17.log','18.log','19.log','20.log','21.log','22.log','23.log','24.log','25.log',
#'26.log','27.log','28.log','29.log','30.log','31.log','32.log','33.log','34.log','35.log','36.log','37.log',
# '38.log','39.log']#,'40.log']#,'41.log','42.log','43.log','44.log','45.log','46.log','47.log','48.log','49.log',
#'50.log']#,'#101.log']
#files=['1','2','3','4','5','6','7',
files=['1.log','2.log','3.log','4.log','5.log','6.log','7.log','8.log','9.log','10.log','11.log','12.log','13.log','14.log','15.log','16.log','17.log'
,'18.log','19.log','20.log','21.log','22.log','23.log','24.log','25.log','26.log','27.log','28.log','29.log','30.log','31.log','32.log','33.log'
,'34.log','35.log','36.log','37.log','38.log','39.log','40.log','41.log','42.log','43.log','44.log','45.log','46.log','47.log','48.log','49.log'
,'50.log','61.log','62.log','63.log','64.log','65.log','66.log','67.log','68.log','69.log','70.log',
'71.log','72.log','73.log','74.log','75.log','76.log','77.log','78.log','79.log','80.log',
'81.log','82.log','83.log','84.log','85.log','86.log','87.log','88.log','89.log','90.log',
'91.log','92.log','93.log','94.log','95.log','96.log','97.log','98.log','99.log','100.log']
## step2: 按照目录获取最小值并plt
plt.figure(figsize=(16,8),dpi=80)
for k in dir:
files=os.listdir(path+k)
# print(files,k)
vt0_min=[] ### list store of min vt value of each dir
slot_name=[]
vt_value_flt_wo0=[]
for p in slotn:
a='zero pattern vth worsest case:'
for i in files:
with open(path+k+'\'+i,'r',encoding='gb18030',errors='ignore') as file:
data=file.readlines()
for n in data:
if a in n:
#print(n)
vt_value_str=data[data.index(n)][31:-2]
#print(vt_value_str)
vt_value_flt_wo0.append(vt_value_str)
print(vt_value_flt_wo0,len(vt_value_flt_wo0))
vt0_min=[float(i) for i in vt_value_flt_wo0]
#slot_name.append(k+'\'+ str(i))
#min_slot=slot_name[vt0_min.index(min(vt0_min))]
print(vt0_min)
x=np.sort(vt0_min)
y=np.arange(1,len(x)+1)/len(x)
plt.yticks(np.arange(-0.05,1.05,0.05)) # set yaxis label precision
plt.plot(x,y,marker='.',linestyle=':',label='%s %d pcs min vt=%f'%(k,len(vt0_min),min(vt0_min)))
plt.annotate(min(vt0_min),(min(vt0_min),1/len(vt0_min)))
plt.yscale('log') # 将Y轴设置为对数刻度
plt.xlabel('vt value last not 0 count')
plt.ylabel('cumulative probability')
plt.legend(loc='lower right',fontsize='small')#'best'
plt.tight_layout()
plt.grid(b=bool)
plt.title('vt distribution')
#plt.show()
t = time.strftime('%Y%m%d-%H%M',time.localtime(time.time()))
plt.tight_layout()
plt.savefig(path1+'min_vt_value_distribution.png')
#plt.savefig(path2+'vtdistr.png')
plt.close()
解释:
- 在代码中,我们在绘制曲线之前添加了以下代码:
plt.yscale('log')
这行代码使用 plt.yscale() 函数将Y轴设置为对数刻度。
- 其他代码部分与原始代码相同,用于读取数据、计算VT值最小值并绘制分布图。
通过以上修改,我们可以将Y轴排列方式改为对数,从而更好地展示数据分布,尤其是当数据集中在较小的范围内时。
注意:
plt.yscale('log')会将Y轴设置为对数刻度,但不会改变Y轴的值。- 在进行对数转换之前,确保Y轴的值都是正数。
- 如果需要更精确的Y轴刻度设置,可以使用
plt.yticks()函数进行调整。
原文地址: https://www.cveoy.top/t/topic/oDF7 著作权归作者所有。请勿转载和采集!