我想从python中的文本文件中绘制两列的图形

问题描述 投票:0回答:1

例如,我有一个像这样的文本文件...。我需要为VPRO列旁边的两列绘制图形。

! Initial pressure in atm
PRES   0.34833240    
! Volume profile
! Time is s and volume in cm3
! Note: start time should be zero, since 
! chemkin 3.7 doesn't make output on data with negative times (error)
VPRO  0.00000000      1.0000000    
VPRO  0.54822008E-02 0.99950299    
VPRO  0.65802743E-02  1.0026889    
VPRO  0.73418149E-02 0.99627431    
VPRO  0.90739698E-02  1.0158893    
VPRO  0.96140946E-02  1.0028384    
VPRO  0.97804742E-02  1.0070171    
VPRO  0.10084646E-01 0.99990454    
VPRO  0.10693270E-01  1.0107573

而且我有这样的代码。但是输出什么也没有显示

import numpy as np
import matplotlib.pyplot as plt
for line in open("textfile.txt", "r").readlines():
    line = line.split()
    if len(line)>1 and line[0] == 'VPRO':
        column1 = line[1]
        column2 = line[2]
plt.plot(column1,column2)
plt.xlim(0,1)
plt.ylim(0,1)
plt.show()
python
1个回答
0
投票

您需要将项目插入数组。 column1 = line[1]仅将当前值设置为column1变量。

import numpy as np
import matplotlib.pyplot as plt

column1 = []
column2 = []

for line in open("d:/test/textfile.txt", "r").readlines():
    line = line.split()
    if len(line)>1 and line[0] == 'VPRO':
        column1.append(line[1])
        column2.append(line[2])

plt.plot(column1,column2)
plt.xlim(0,1)
plt.ylim(0,1)
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.