簡體   English   中英

從 python 中的數據文件讀取和繪圖

[英]Reading and plotting from a data file in python

我是 python 的初學者,我有一些問題需要您的幫助。 如果問題太基本,我很抱歉。

我有一個如下所示的數據文件(文件很長,超過 4000 行);

4
0.1 0.1
1.5 0.1
0.5 0.6
1.5 0.6
0.0000000E+00 0.0000000E+00 0.0000000E+00 -1.3180656E-04
0.0000000E+00 0.0000000E+00 0.0000000E+00 -2.8582822E-05
0.0000000E+00 0.0000000E+00 0.0000000E+00 -1.5833791E-04
0.0000000E+00 0.0000000E+00 0.0000000E+00 -9.9146621E-05
2.5294579E-02 -3.7180660E-01 7.5958102E-02 6.4079851E-01
2.5294579E-02 7.1739070E+00 6.4493904E-01 6.6945873E-01
2.5294579E-02 3.1476634E+00 8.8396035E-01 -1.4551238E-02
2.5294579E-02 1.6825711E+00 5.1869466E+00 2.4610339E-02
3.2829473E-02 -5.1518883E+00 5.4573026E+00 6.7564747E-01
3.2829473E-02 1.9206643E+01 -1.2400739E+00 6.9728887E-01
3.2829473E-02 -1.5529481E+01 4.6126603E+00 -5.2802531E-03
3.2829473E-02 -4.3019722E+00 7.6228330E+00 5.4802021E-02
3.6500080E-02 -1.0096638E+01 1.1882060E+01 7.0272428E-01
3.6500080E-02 2.8727686E+01 -7.0275729E+00 6.5145852E-01
3.6500080E-02 -3.7448674E+01 -4.4589296E+00 2.2430999E-02
3.6500080E-02 -1.1295979E+01 5.3019553E+00 8.0316341E-02

我想寫一個 python 代碼讀取第 6 行,然后跳過接下來的三行並讀取第 10 行,然后是第 14 行,依此類推(換句話說,代碼應該跳過前五行,然后讀取第 6 行跳過接下來的 3 行,閱讀下一行,依此類推)。

然后代碼應該使用這些讀取行到 plot 沿 y 軸的第二、第三和第四列以及沿 x 軸的第一列。

我試圖編寫下面的代碼,但它並沒有完全符合我的要求。

import numpy as np 
import matplotlib.pyplot as plt 
from math import* 
import scipy.optimize as sci 
#loading data 
with open('data.his') as f: 
d=f.readlines()[5:] 
#assigning the columns 
time=d[:,0] 
vx=d[:,1] 
vy=d[:,2] 
temp=d[:,3] 
# plotting
plt.plot(time,temp, label='Temperature variation') 
plt.xlabel('Time [s]') 
plt.ylabel('Temperature [K]') 
plt.legend() 
plt.show() 

預先感謝您的協助。

您可以使用模運算符%僅選擇每四行。

time = []

for j, c in enumerate(d):
    if j % 4 == 0:
        time.append(c[0])

vxvytemp相同(使用相應的列索引)。

TL;博士

data  = dict(zip(('x', 'y1', 'y2', 'y3', ...), # as many as your columns
                 (c for c in zip(*((float(s) for s in l.split())
                      for n, l in enumerate(open(...)) if n>4 and not (n-5)%4)))))
plt.plot(cols['x'], cols['y1'], label='Y1')
...
plt.legend() ; plt.show()

展開的、注釋良好的初始代碼

你可以用許多不同的方式做你想做的事,我個人會這樣開始(請閱讀內聯注釋以了解代碼的解釋),然后......

# open the file and slurp its contents
with f as open(...):
    lines = f.readlines()

# throw away the uninteresting lines,
# using slice notation, [5::4] means:
#   from line #6 ("[5:", Python counts from 0) to the end ("::") with a step of 4 (":4]")
lines = lines[5::4]

# turn space separated strings into floats
# for every line in the interesting lines, split it and
# make a list containing the numerical values
# numbers is a list of lists, each list corresponding to a line in the file
numbers = [[float(s) for s in line.split()] for line in lines]

# transpose the numbers, i.e., turn columns into rows, so that now
# numbers is a list of lists, each list corresponding to a COLUMN of data
numbers = [column for column in zip(*numbers)]

# plot the relevant columns (remember that Python counts from 0)
plt.plot(numbers[0], numbers[1], label='1')
plt.plot(numbers[0], numbers[2], label='2')
...
plt.legend()

一、合理優化

代替

lines = lines[5::3]
numbers = [[float(s) for s in line.split()] for line in lines]

您可能應該將這兩個語句合並為

numbers = [[float(s) for s in line.split()] for line in lines[5::3]]

我在示例代碼中使用了兩個語句,因為這樣更容易進行適當的注釋。


不太合理的優化

進一步向下進入合並的狹窄路徑,

cols = [c for c in zip(*((float(s)for s in l.split())for n,l in enumerate(open(...))if n>4 and not (n-5)%4))]
plt.plot(cols[0], cols[1], label='1')
...

最后的優化,不無道理...

使用字典!

data  = dict(zip(('x', 'y1', 'y2', 'y3', ...), # as many as your columns
                 (c for c in zip(*((float(s) for s in l.split())
                      for n, l in enumerate(open(...)) if n>4 and not (n-5)%4)))))
plt.plot(data['x'], data['y1'], label='Y1')
...
plt.legend() ; plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM