Pandas 时间序列
试图在Y轴上用值在x轴上绘制YYYY:MM:DD HH:MM:SS “df=xxxx”是Pandas 时间序列,pandas,matplotlib,Pandas,Matplotlib,试图在Y轴上用值在x轴上绘制YYYY:MM:DD HH:MM:SS “df=xxxx”是 tagName tagValue tagTimestamp 0 Oil Pressure 52.512268 2018-09-17 12:20:03.099 1 Oil Pressure 52.443478 2018-09-17 12:20:02.598 2 Oil Pressure 48.912914 2018-09-17 12:20:02.3
tagName tagValue tagTimestamp
0 Oil Pressure 52.512268 2018-09-17 12:20:03.099
1 Oil Pressure 52.443478 2018-09-17 12:20:02.598
2 Oil Pressure 48.912914 2018-09-17 12:20:02.348
4 Oil Pressure 45.463978 2018-09-17 12:20:01.848
5 Oil Pressure 50.580151 2018-09-17 12:20:01.598
6 Oil Pressure 49.411255 2018-09-17 12:20:01.348
8 Oil Pressure 48.072506 2018-09-17 12:20:01.146
运行df.plot(kind='scatter',x='tagTimestamp',y='tagvalue',color='red')
返回错误ValueError:scatter要求x列为数字
我希望将整个日期时间保留在x列中。我已经审阅了与本主题密切相关的所有堆栈帖子,但未能成功地将其转换并打印出来
df.d类型:
tagName object
tagValue float64
tagTimestamp datetime64[ns]
dtype: object
这对我有用,这就是你想要的吗
import pandas as pd
data.dtypes
给出:
tagValue float64
tagTimestamp datetime64[ns]
dtype: object
以下是数据:
tagValue tagTimestamp
0 52.512268 2018-09-17 12:20:03.099
1 52.443478 2018-09-17 12:20:02.598
2 48.912914 2018-09-17 12:20:02.348
3 45.463978 2018-09-17 12:20:01.848
4 50.580151 2018-09-17 12:20:01.598
5 49.411255 2018-09-17 12:20:01.348
6 48.072506 2018-09-17 12:20:01.146
然后以折线图而不是散点图的形式绘制:
data.plot(x = 'tagTimestamp')
给出:
tagValue float64
tagTimestamp datetime64[ns]
dtype: object
您可以使用matplotlib的
散点
plt.scatter(df["tagTimestamp"].values, df["tagValue"].values)
完整示例:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
t = ["2018-09-17 12:20:03.099", "2018-09-17 12:20:02.598", "2018-09-17 12:20:02.348", "2018-09-17 12:20:01.848",
"2018-09-17 12:20:01.598", "2018-09-17 12:20:01.348", "2018-09-17 12:20:01.146"]
df = pd.DataFrame({"time" : t, "value" : np.random.rand(len(t))})
df["time"] = pd.to_datetime(df["time"])
print(df.dtypes) # time datetime64[ns]
# value float64
# dtype: object
plt.scatter(df["time"].values, df["value"], color="red")
X轴缺少值且间距不均匀。您不能将其转换为连续查看的比例,以获得当前的趋势图。