繁体   English   中英

在python中绘制txt文件中的选定值

[英]Plotting selected values from txt file in python

我有一个txt文件,其中包含2016-2018年每一天的每一秒对应的数据(每天结束约1400多条数据),首先,我选择了特定日期的数据:05.01.2016,然后我想使用当天的数据在python中绘制图形

以下代码用于选择值和绘制图形:

if '01.05.2016' in row: #select the value in day 05.01.2016
    x = [row.split()[6]] 
    y = [row.split()[2]]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x,y)
plt.ylim((200,800))
plt.show()

如果我使用命令:

x = [row.split()[6]] 
y = [row.split()[2]]
print(x,y)

那么 x 和 y 就会成功出来(只是一些对 xy 值作为例子):

['01.05.2016/15:43:00'] ['499']
['01.05.2016/15:44:00'] ['501']
['01.05.2016/15:45:00'] ['502']
['01.05.2016/15:46:00'] ['502']

我原来的 txt.file 的一部分是这样的:

01.05.2016  15:43:00    499 U   42491,65486  -0,01   01.05.2016/15:44:00
01.05.2016  15:44:00    501 U   42491,65556  0,01   01.05.2016/15:45:00 
01.05.2016  15:45:00    502 U   42491,65625  0,02   01.05.2016/15:46:00 
01.05.2016  15:46:00    503 U   42491,65694  0,03   01.05.2016/15:47:00 

但是如果我继续编写绘制图形的命令。 该图仅显示一对(最后一对)xy 值,我的图如下所示:

有一些错误的图表

谁能帮帮我?

你可以这样做:

import matplotlib.pyplot as plt
df = pd.read_csv('file.txt', sep = '\t', names = ["col", "col1", "val", "col3", "col4", "time"])
df['time'] = pd.to_datetime(df['time'], format='%d.%m.%Y/%H:%M:%S')
df.set_index('time', inplace=True)
match_timestamp = "01.05.2016"
df1 = df.loc[df.index.strftime("%m.%d.%Y") == match_timestamp]
print df1
df1['val'].plot()
plt.show()

小心哪个是pd.read_csv的分隔符

例子

输入:

01.04.2016  15:46:00    503 42491,65694 0,03    02.05.2016/15:47:00
01.05.2016  15:43:00    499 42491,65486 -0,01   01.05.2016/15:44:00
01.05.2016  15:44:00    501 42491,65556 0,01    01.05.2016/15:45:00 
01.05.2016  15:45:00    502 42491,65625 0,02    01.05.2016/15:46:00 
01.05.2016  15:46:00    503 42491,65694 0,03    01.05.2016/15:47:00 
02.05.2016  15:46:00    503 42491,65694 0,03    02.05.2016/15:47:00

df1:

                            col      col1   val         col3   col4
time                                                               
2016-01-05 15:44:00  01.05.2016  15:43:00   499  42491,65486  -0,01
2016-01-05 15:45:00  01.05.2016  15:44:00   501  42491,65556   0,01
2016-01-05 15:46:00  01.05.2016  15:45:00   502  42491,65625   0,02
2016-01-05 15:47:00  01.05.2016  15:46:00   503  42491,65694   0,03

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM