简体   繁体   English

Matplotlib绘图时间序列图

[英]Matplotlib plot time series graph

I have a pandas dataframe with 2 columns: 我有2列的pandas数据框:

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? 如何绘制折线图,​​其中x轴是时间,y轴是值? I tried: 我试过了:

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

But I got an error message: ValueError: invalid literal for float: 18:03 . 但是我收到一条错误消息:ValueError: float的无效文字:18:03 I converted the column to datetime format and tried plot_date, but all failed 我将列转换为日期时间格式并尝试了plot_date,但都失败了

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 您需要确保正确使用datetime库,例如,如果您的时间以“ HH:MM”格式的字符串来表示,则应将其拆分并执行以下操作

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 大熊猫有自己的绘图功能,可以为您绘制线条

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

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