【问题标题】:question about plotting from txt file datas关于从 txt 文件数据绘图的问题
【发布时间】:2018-09-25 22:34:27
【问题描述】:

txt 文件中绘制数据时出现问题。我的txt 文件中有 7 列,我想使用最后一列和第三列(分别作为 x 轴和 y 轴)的数据,但该命令不起作用。

错误是:

x = [row.split()[6] for row in data]
> IndexError: list index out of range

我的代码是:

x = [row.split()[6] for row in data]
y = [row.split()[2] for row in data]

index = [i for i,val in enumerate(x)]

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xticklabels(x)
ax1.plot(index ,y, c='r')
leg = ax1.legend()
plt.locator_params(nbins=len(index)-1)
plt.show()

这是我的txt 文件的一部分。

01.05.2016  00:01:00    313 U   42491,00069 -1,87   01.05.2016 00:02

01.05.2016  00:02:00    313 U   42491,00139 -1,87   01.05.2016 00:03

01.05.2016  00:03:00    313 U   42491,00208 -1,87   01.05.2016 00:04

如有必要,以下代码显示了我如何初始化数据:

import matplotlib.pyplot as plt
with open("tesy=t.txt") as f:
    data = f.read()
    data = data.split('\n')

【问题讨论】:

  • 您的输入中是否有任何较短的/空白行?
  • 尝试打印 row.split() 结果。你可以看到列。
  • 一旦你拆分,你有 8 列,所以最后一列将是 [7] 索引
  • 您确定正确加载数据吗?
  • 请告诉我们你是如何初始化data variabe的?

标签: python matplotlib


【解决方案1】:

我在文件file.txt 中使用了你的三行数据,根据你的描述,这是我通过重写你的代码得到的情节。您不需要名为 index 的附加变量来标记您的 x-ticks。您可以直接绘制x,x-tick 标签将自动设置。要访问最后一列,我使用索引[-1]。这避免了计算列和手动放置索引。我删除了图例,因为您在绘图命令中没有定义图例。

file = open("file.txt", 'r')
data = file.readlines()
data[0].split()
x = [row.split()[-1] for row in data] # if no blank line in the file
y = [row.split()[2] for row in data] # if no blank line in the file
# x = [row.split()[-1] for row in data if row.strip()] # if blank lines in the file
# y = [row.split()[2] for row in data if row.strip()] # if blank lines in the file

index = range(len(x))
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x ,y, '-ro')

输出

【讨论】:

  • 谢谢你的回复,我刚才试了下你的代码,图显示了,但是xaxis有一些问题,y值有很多重叠,我想是因为xaxis是秒单位,但是txt文件中有很多天,如果xaxis不是'日期和时间'单位,那么图表不完美。所以我将x = [row.split()[-1] for row in data] 更改为x = [row.split()[6] for row in data],然后图表更好。虽然我不知道 -1 和 6 之间的区别,但这毕竟是有效的。非常感谢!
【解决方案2】:

你可以试试这个,这应该用于你的文件,并逐行读取,然后从每行的最后一列中提取 x 值并将其附加到列表 x 中,并从第 3 列中提取 y 值并将其附加到列表 y 之后,您将保存所有数据(x 和 y)并列出,现在您可以绘图了

x = []
y = []
filename = '/data.txt'

with open(filename) as f:
    x = [x.split(' ')[-1] for x in f.readlines()]
with open(filename) as f:
    y = [x.split(' ')[2] for x in f.readlines()]

这将导致一个包含\n 值的列表,它指的是一个新行

为了避免这种情况,你可以这样做

x = []
y = []
filename = '/data.txt'

with open(filename) as f:
    x = [x.split(' ')[-1] for x in f.read().splitlines()]
with open(filename) as f:
    y = [x.split(' ')[2] for x in f.read().splitlines()]
print(x)
print(y)

plt.figure(1)
plt.plot(x, y, marker='o')
plt.show()

输出将是

['00:02', '00:03', '00:04']
['313', '313', '313']

【讨论】:

  • 感谢您的回复!我厌倦了你的代码,但这在我的电脑上运行不成功,错误是:x and y must have same first dimension, but have shapes (1048575,) and (0,) 我认为该命令无法在我的电脑上输出 y 值。
  • 究竟在哪一行?
  • 最后3行是:File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 242, in _xy_from_xy "have shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y must have same first dimension, but have shapes (1048575,) and (0,)
  • 谢谢,但还是有错误:File "C:/Users/AK6PRAKT/Desktop/Daten.sammeln.Aufgabe/neletxt.py", line 15, in <listcomp> y = [x.split(' ')[4] for x in f.read().splitlines()] IndexError: list index out of range
  • 谢谢你,但总是同样的错误:File "C:/Users/AK6PRAKT/Desktop/Daten.sammeln.Aufgabe/neletxt.py", line 15, in <listcomp> y = [x.split(' ')[2] for x in f.read().splitlines()] IndexError: list index out of range 我想也许正如@Bazingaa 所说,我只需要写x = [row.split()[-1] for row in data] y = [row.split()[2] for row in data],我已经更改了最后一列在我的 txt 文件中,我删除了 / 而不是 Blank 现在图表显示。
猜你喜欢
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-25
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
  • 2016-07-22
相关资源
最近更新 更多