如何从python中的文本文件中为两列绘制图形?

如何从python中的文本文件中为两列绘制图形?,python,graph,visualization,Python,Graph,Visualization,我有一个这样的文本文件,我需要为最后两列绘制一个图表: ! 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.0000000

我有一个这样的文本文件,我需要为最后两列绘制一个图表:

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

您需要将项插入到数组中
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()

您需要将项插入到数组中
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()

我们还可以读取文件并删除前6行,然后使用空格作为分隔符直接将其读入CSV。我们还可以读取文件并删除前6行,然后使用空格作为分隔符直接将其读入CSV。然后简单地绘制列。