Python 操作.dat文件并打印累积数据
我想从一个乏味的.datPython 操作.dat文件并打印累积数据,python,numpy,matplotlib,dataframe,data-manipulation,Python,Numpy,Matplotlib,Dataframe,Data Manipulation,我想从一个乏味的.dat文件中绘制一个量,文件中的#time列从0扩展到70,但我需要仔细查看从25到35秒的数据(在本例中是核能) 我想知道是否有一种方法可以操纵时间列和相应的其他列,只记录和绘制所需时间跨度的数据 我已经有了一些代码,可以在0-70年代为我完成这项工作: import matplotlib matplotlib.use('Agg') import os import numpy as np import matplotlib.pyplot as plt import str
文件中绘制一个量,文件中的#time列从0扩展到70,但我需要仔细查看从25到35秒的数据(在本例中是核能)
我想知道是否有一种方法可以操纵时间列和相应的其他列,只记录和绘制所需时间跨度的数据
我已经有了一些代码,可以在0-70年代为我完成这项工作:
import matplotlib
matplotlib.use('Agg')
import os
import numpy as np
import matplotlib.pyplot as plt
import string
import math
# reads from flash.dat
def getQuantity(folder, basename, varlist):
# quantities[0] should contain only the quantities of varlist[0]
quantities =[]
for i in range(len(varlist)):
quantities.append([])
with open(folder + "/" + basename + ".dat", 'r') as f: # same as f = open(...) but closes the file afterwards.
for line in f:
if not ('#' or 'Inf') in line: # the first line and restarting lines look like this.
for i in range(len(varlist)):
if(varlist[i]==NUCLEAR_ENERGY and len(quantities[i])>0):
quantities[i].append(float(line.split()[varlist[i]])+quantities[i][-1])
else:
quantities[i].append(float(line.split()[varlist[i]]))
return quantities
# end def getQuantity
#create plot
plt.figure(1)
TIME = 0
NUCLEAR_ENERGY = 18
labels = ["time", "Nuclear Energy"]
flashFolder1 = '/home/trina/Pictures' # should be the flash NOT the flash/object folder.
lab1 = '176'
filename = 'flash' # 'flash' for flash.dat
nHorizontal = 1 # number of Plots in Horizontal Direction. Vertical Direction is set by program.
outputFilename = 'QuantityPlots_Nuclear.png'
variables = [NUCLEAR_ENERGY]
#Adjustments to set the size
nVertical = math.ceil(float(len(variables))/nHorizontal) # = 6 for 16 = len(variables) & nHorizontal = 3.
F = plt.gcf() #get figure
DPI = F.get_dpi()
DefaultSize = F.get_size_inches()
F.set_size_inches( DefaultSize[0]*nHorizontal, DefaultSize[1]*nVertical ) #build no of subplots in figure
variables.insert(0,TIME) # time as needed as well
data1 = getQuantity(flashFolder1, filename, variables)
time1 = np.array(data1[0]) #time is first column
for n in [n+1 for n in range(len(variables)-1)]: #starts at 1
ax=plt.subplot(nVertical, nHorizontal, n) #for example (6,3,0 to 15) inside loop for 16 variables
if (min(data1[n])<0.0 or abs((min(data1[n]))/(max(data1[n])))>=1.e-2):
plt.plot(time1, data1[n],label=lab1) #, label = labels[variables[n]])
legend = ax.legend(loc='upper right', frameon=False)
else:
plt.semilogy(time1, data1[n],label=lab1) #, label = labels[variables[n]])
legend = ax.legend(loc='upper right', frameon=False)
plt.savefig(outputFilename)
导入matplotlib
matplotlib.use('Agg')
导入操作系统
将numpy作为np导入
将matplotlib.pyplot作为plt导入
导入字符串
输入数学
#从flash.dat读取
def getQuantity(文件夹、基本名称、变量列表):
#数量[0]应仅包含varlist[0]的数量
数量=[]
对于范围内的i(len(varlist)):
数量。追加([])
将open(folder+“/”+basename+“.dat”,“r”)作为f:#与f=open(…)相同,但随后关闭文件。
对于f中的行:
如果行中没有(“#”或“Inf”):#第一行和重新启动行如下所示。
对于范围内的i(len(varlist)):
如果(varlist[i]==核能和len(数量[i])>0:
数量[i].append(float(line.split()[varlist[i]])+数量[i][1])
其他:
数量[i].append(float(line.split()[varlist[i]]))
退货数量
#结束def获取数量
#创建绘图
plt.图(1)
时间=0
核能=18
标签=[“时间”,“核能”]
flashFolder1='/home/trina/Pictures'#应该是flash而不是flash/object文件夹。
lab1='176'
filename='flash'#'flash'用于flash.dat
nHorizontal=1#水平方向上的绘图数量。垂直方向由程序设置。
outputFilename='QuantityPlots\u Nuclear.png'
变量=[核能]
#调整以设置大小
nVertical=math.ceil(float(len(variables))/nHorizontal)#=6表示16=len(variables)&nHorizontal=3。
F=plt.gcf()#获取数字
DPI=F.获取DPI()
DefaultSize=F.获取大小英寸()
F.设置尺寸(默认尺寸[0]*n水平,默认尺寸[1]*n垂直)#图中子地块的建造数量
变量。根据需要插入(0,时间)#时间
data1=getQuantity(flashFolder1、文件名、变量)
time1=np.array(data1[0])#time是第一列
对于n in[n+1对于n in范围(len(变量)-1)]:#从1开始
ax=plt.子批次(nVertical,nHorizontal,n)#例如16个变量的循环内部(6,3,0到15)
如果(min(data1[n])=1.e-2):
plt.plot(time1,data1[n],label=lab1)#,label=lables[variables[n]]
图例=ax.图例(loc='右上角',frameon=False)
其他:
plt.符号学(time1,data1[n],label=lab1)#,label=lables[variables[n]]
图例=ax.图例(loc='右上角',frameon=False)
plt.savefig(输出文件名)
下面是我可以从这段代码生成的图:
为了您的方便,我也在共享.dat
文件:
非常感谢您的建议。更新:绘制累积核能:
x = df.query('25 <= time <= 35').set_index('time')
x['cum_nucl_energy'] = x.Nuclear_Energy.cumsum()
x.cum_nucl_energy.plot(figsize=(12,10))
x=df.query('25更新:绘制累积核能:
x = df.query('25 <= time <= 35').set_index('time')
x['cum_nucl_energy'] = x.Nuclear_Energy.cumsum()
x.cum_nucl_energy.plot(figsize=(12,10))
x=df.query('25我找不到Nuclear Energy
column…要绘制哪一列?该列是.dat文件中的第19列,名为“increment”你不使用熊猫的原因是什么?这是一种可以让你的生活更轻松的情况。嗯,我可以使用熊猫,但我只是不知道如何操作熊猫中的列,我使用了完整的数据来绘制相同的图形。我找不到核能列…你想绘制什么列?那是cColumn是.dat文件中的第19个,名为“increment”,无论您为什么不使用pandas
?这是一种可以让您的生活更轻松的情况。嗯,我可以使用pandas来实现这一点,但我只是不知道如何操作pandas中的列,我使用了完整的数据来绘制相同的结果figure@bhjghjh,很高兴我能帮忙:).我发布了一个更好的版本,使用了ggplot
样式…非常感谢,不过我有一点疑问,这个数字表示每个时间步的数据,有没有办法让它累积起来,就像我在我的图中所做的那样?@bhjghj,你能解释一下你是如何让它“累积”的吗?好的,就像每个时间步一样,它会将数据与之前的时间步数据一起添加,比如:sum=newsum+sum,这样它会给你累计数据……(对不起,我不是程序员,不知道它的确切名称,英语也不是我的第一语言)@bhjghhh,我已经更新了我的答案-这是你想要的吗?@bhjghhh,很高兴我能帮上忙:). 我发布了一个更好的版本,使用了ggplot
风格…非常感谢,不过我有一个小小的疑问,这个数字表示每个时间步的数据,有没有办法让它累积,就像我在我的数字中所做的那样?@bhjghj,你能解释一下你是如何让它“累积”的吗,就像每个时间步一样,它会将数据与之前的时间步数据一起添加,比如:sum=newsum+sum,这样它会给你累计数据……(对不起,我不是程序员,不知道它的确切名称,英语也不是我的第一语言)@bhjghj,我已经更新了我的答案-这就是你想要的吗?
In [43]: pd.options.display.max_rows
Out[43]: 50
In [44]: pd.options.display.max_rows = 12
In [45]: df
Out[45]:
time Nuclear_Energy
0 0.000000e+00 0.000000e+00
1 1.000000e-07 -4.750169e+29
2 2.200000e-07 -5.699325e+29
3 3.640000e-07 -6.838392e+29
4 5.368000e-07 -8.206028e+29
5 7.441600e-07 -9.837617e+29
... ... ...
10210 6.046702e+01 7.160630e+44
10211 6.047419e+01 7.038907e+44
10212 6.048137e+01 6.934600e+44
10213 6.048856e+01 6.847015e+44
10214 6.049577e+01 6.765220e+44
10215 6.050298e+01 6.661930e+44
[10216 rows x 2 columns]
In [46]: df.query('25 <= time <= 35')
Out[46]:
time Nuclear_Energy
4534 25.001663 1.559398e+43
4535 25.006781 1.567793e+43
4536 25.011900 1.575844e+43
4537 25.017021 1.583984e+43
4538 25.022141 1.592015e+43
4539 25.027259 1.600200e+43
... ... ...
6521 34.966427 8.181516e+41
6522 34.972926 8.538806e+41
6523 34.979425 8.913695e+41
6524 34.985925 9.304403e+41
6525 34.992429 9.731310e+41
6526 34.998941 1.019862e+42
[1993 rows x 2 columns]
In [47]: df.query('25 <= time <= 35').set_index('time')
Out[47]:
Nuclear_Energy
time
25.001663 1.559398e+43
25.006781 1.567793e+43
25.011900 1.575844e+43
25.017021 1.583984e+43
25.022141 1.592015e+43
25.027259 1.600200e+43
... ...
34.966427 8.181516e+41
34.972926 8.538806e+41
34.979425 8.913695e+41
34.985925 9.304403e+41
34.992429 9.731310e+41
34.998941 1.019862e+42
[1993 rows x 1 columns]