Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python中的txt文件中提取两列数字并绘制它们_Python_Pycharm - Fatal编程技术网

从python中的txt文件中提取两列数字并绘制它们

从python中的txt文件中提取两列数字并绘制它们,python,pycharm,Python,Pycharm,我需要一些PyCharm中Python的基本帮助。我试图从一个.txt文件中提取两列数字(但每列中的数字数量会发生变化),然后绘制它们。到目前为止,这是我的代码 pacient = str(input("Please enter your the name of the pacient: ")) Pname = pacient+'.txt' print(Pname) file = open(Pname,"r") print (file.read()) # what i need in her

我需要一些PyCharm中Python的基本帮助。我试图从一个.txt文件中提取两列数字(但每列中的数字数量会发生变化),然后绘制它们。到目前为止,这是我的代码

pacient = str(input("Please enter your the name of the pacient: "))
Pname = pacient+'.txt'
print(Pname)
file  = open(Pname,"r")
print (file.read())

# what i need in here is save the first column of the .txt in 't' and the second one in 'v'. 



import matplotlib.pyplot as plt
plt.plot(t, v)
plt.xlabel('time (v)')
plt.ylabel('Velocity (m/s)')
plt.title(pacient+"plot")
plt.savefig("test.png")
plt.show()

您可以使用
csv
模块读取文件:

import csv

t = []
v = []
with open(Pname, "r") as patient_f:
    csv_f = csv.reader(patient_f, delimieter='delimiter_between_columns')
    for row in csv_f:
        t.append(row[0])
        v.append(row[1])

您可以使用numpy为您排序列:

import numpy as np
file = np.loadtxt("filename.txt", delimiter=',') #do not need delimiter if your file is not csv. 
t = file[:,0]
v = [:,1]

plt.plot(t, v)
plt.show()
plt.xlabel('time (v)')
plt.ylabel('Velocity (m/s)')
plt.title(pacient+"plot")
plt.savefig("test.png")
plt.show()
没有numpy的另一种方式:

file = open('filename.txt').readlines()
file = [map(int, i.strip('\n').split()) for i in file]
new_data = [list(i) for i in zip(*file)]

plt.plot(new_data[0], new_data[1])
plt.show()