2

I have a txt file which contains the datas corresponding to every second in every single day from year 2016-2018(everyday concluding about over 1400 datas), firstly, I selected the datas on a specifical day: 05.01.2016, then I want to draw a graph in python using the day's datas

the following code is for selecting values and drawing graph:

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()

if I use the command:

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

then the x and y come will successfully come out(just some pairs x-y values as example):

['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']

a part of my original txt.file is like:

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 

But if I continue to write the command for drawing graph. the graph only show one pair(the last one pair) x-y value, my graph is like:

graph with some errors

who can help me maybe?

13
  • Do you want to plot just the 4 x-y values listed above? Commented Sep 24, 2018 at 12:40
  • @Bazingaa Thanks for your replying. Nope, the 4 lines are only example, actually there are about 1400 x-y values in 01.05.2616, I want it all as my x-y values. Commented Sep 24, 2018 at 12:44
  • Then show us what figure you are currently getting? It's hard to imagine what might be your problem Commented Sep 24, 2018 at 12:45
  • 1
    @It'sNele Use this: df['time'] = pd.to_datetime(df['time'], format='%d.%m.%Y/%H:%M:%S') Commented Sep 24, 2018 at 14:53
  • 1
    @Joe OMG, that works so good, thanks a lot!!! :))))))))))))))))))) Commented Sep 24, 2018 at 14:55

1 Answer 1

1

You could do in this way:

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()

Be careful which is your separator in pd.read_csv

Example

Input:

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.