2016-09-18 1 views
1

Ich möchte eine Menge von einer langweiligen zu sehen-at .dat Datei, die Spalte #time in der Datei erstreckt sich von 0 bis 70s, aber ich muss näher kommen Betrachten Sie die Daten (in diesem Fall Kernenergie) von 25s bis 35s.eine .dat Datei manipulieren und kumulative Daten plotten

Ich frage mich, ob es eine Möglichkeit gibt, die Zeitspalte und entsprechende andere Spalten zu manipulieren, um Daten nur für die erforderliche Zeitspanne aufzuzeichnen und zu plotten.

ich bereits einen Code haben, die für mich den Job für 0-70s:

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) 

Hier ist die Figur, die ich aus diesem Code erzeugen kann:

enter image description here

und für Ihre Bequemlichkeit teile ich auch die .dat Datei:

https://www.dropbox.com/s/w4jbxmln9e83355/flash.dat?dl=0

Ihre Vorschläge werden am meisten geschätzt.

+0

ich nicht 'Nuclear Energy' Spalte finden konnte ... Welche Spalte sehen Sie zeichnen möchten? – MaxU

+0

diese Spalte ist die 19. in .DAT-Datei, mit dem Namen "Inkrement" – bhjghjh

+0

Jeder Grund, warum Sie nicht 'Pandas' verwenden? Dies ist eine dieser Situationen, in denen es Ihr Leben viel einfacher machen kann. –

Antwort

4

UPDATE: Grundstück kumulative Kernenergie:

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)) 

enter image description here

Alte Antwort:

Mit Pandas Modul

import pandas as pd 
import matplotlib 
import matplotlib.pyplot as plt 

matplotlib.style.use('ggplot') 

fn = r'D:\temp\.data\flash.dat' 
df = pd.read_csv(fn, sep='\s+', usecols=[0, 18], header=None, skiprows=[0], na_values=['Infinity']) 
df.columns=['time', 'Nuclear_Energy'] 
df.query('25 <= time <= 35').set_index('time').plot(figsize=(12,10)) 
plt.show() 
plt.savefig('d:/temp/out.png') 

Ergebnis:

enter image description here

Erläuterung:

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] 
+0

du bist ein Lebensretter-Mann! – bhjghjh

+0

@bhjghjh, froh, ich könnte helfen :). Ich habe eine etwas schönere Version gepostet, mit 'ggplot' style ... – MaxU

+0

vielen Dank, ich habe eine kleine Frage, obwohl diese Zahl Daten bei jedem Zeitschritt darstellt, gibt es eine Möglichkeit, es kumulativ zu machen, wie ich es tat in meiner Figur? – bhjghjh

Verwandte Themen