1

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

2
  • How you converted to datetime format ? Commented Jul 7, 2017 at 6:56
  • pandas to_datetime Commented Jul 7, 2017 at 6:57

1 Answer 1

1

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

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.