ELI
ELI

Reputation: 369

Matplotlib plot time series graph

I have a pandas dataframe with 2 columns:

Time     Values
07:40         5
08:10         6
08:25         3
08:53         4
...         ...

How can I plot a line chart which x-axis is the time and y-axis is values? I tried:

plt.plot(df["Time"],df["Values"])

But I got an error message: ValueError: invalid literal for float: 18:03. I converted the column to datetime format and tried plot_date, but all failed

Upvotes: 1

Views: 1700

Answers (1)

Hamuel
Hamuel

Reputation: 633

you need to make sure you use the datetime library properly for example if your time come as a string of the format "HH:MM" you should split it and do the following

import datetime

def process_time(x):
  h, m = x.split(":")
  return datetime.time(hour=int(h), minute=int(m))

df['Time'].apply(process_time)

df.plot(x='Time', y='Values') 

pandas have it own plot function which will plot the line for you

Upvotes: 1

Related Questions