我有一个类似这样的文本文件,我需要为最后两列绘制一个图形:
! 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()发布于 2019-12-09 19:50:29
您需要向数组中插入项。column1 = line[1]此选项仅将当前值设置为column1变量。另外,将文本值转换为适当的类型,如float。
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(float(line[1]))
column2.append(float(line[2]))
plt.plot(column1,column2)
plt.xlim(0,1)
plt.ylim(0,1)
plt.show()https://stackoverflow.com/questions/59248141
复制相似问题