仅使用部分逗号分隔的文件

2024-04-25 09:16:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道在Python中,如果有一个逗号分隔的文件

1,5
2,4
3,3
4,2
5,1

您可以执行以下类似操作:

import numpy as np

x, y = np.loadtxt('example.txt', delimiter=',', unpack=True)
plt.plot(x,y, label='myLine')

这将绘制数据的X,Y线;然而,我很好奇-我有一个CDF,它设置了3列,比如

1/1/2016, 5, 0
1/2/2016, 4, 1
1/3/2016, 3, 2
1/4/2016, 2, 3
1/5/2016, 1, 4
1/6/2016, 0, 5

我想把日期画成X,第二列是Y1,第三列是Y2,这样在同一张图上就有两行了。我对Python还是个新手,我知道如何静态地绘制多行的图形,但我的问题是,是否有一种方法可以一次只获取一个文件的一部分数据,而不是Python期望每行的整个序列,而是构建一个X,Y,Z图形。你知道吗


Tags: 文件数据importnumpytxttrue图形example
1条回答
网友
1楼 · 发布于 2024-04-25 09:16:16

可以将^{}usecols=(1, 2)一起使用:

usecols : sequence, optional - Which columns to read, with 0 being the first. For example, usecols = (1,4,5) will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read.

相关问题 更多 >