在python中读取和绘制多个txt文件 [英] read and plot multiple txt files in python

查看:46
本文介绍了在python中读取和绘制多个txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些输出文件,即 frequency1 .txt、frequency2 .txt 等等(直到 21).在每个txt文件中,我有10列,假设有n行,现在我需要为所有这些txt文件绘制第2列和第3列.我能够为单个txt文件绘制

I have some output files ie frequency1 .txt , frequency2 .txt and so on (till 21). In each txt files I am having 10 columns and suppose n rows , now I need to plot column 2 and column 3 for all these txt files .I am able to plot for a single txt file

import numpy as np
from matplotlib import pyplot as plt

data=np.loadtxt('frequecy1.txt')
pl.plot(data[:,1],data[:,2],'bo')
X=data[:,1]
Y=data[:,2]
plt.plot(X,Y,':ro')
plt.ylim((0,55000))
plt.show()

我将如何绘制所有文件?

How would I plot all the files?

推荐答案

只是想在@Korem 的答案中添加一些内容.您可以创建文件名的列表,然后使用 for 循环将其传递,而不是手动编写文件名.

Just wanted to add something to @Korem's answer. You can create a list of filenames then pass it using for loop instead of writing file names manually.

import numpy as np
import matplotlib.pyplot as plt

filelist=[]

for i in range(1,5):
    filelist.append("frequency%s.dat" %i)

for fname in filelist:
    data=np.loadtxt(fname)
    X=data[:,0]
    Y=data[:,1]
    plt.plot(X,Y,':ro')

plt.show()

这篇关于在python中读取和绘制多个txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆