简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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