简体   繁体   English

如何绘制趋势线作为日期的函数?

[英]How to Graph Trend Line as function of Dates?

I'm trying to plot a trend line that depends on dates (in the form Sep-14 to Dec-2018) on the same plot as my actual data values.我试图在与我的实际数据值相同的图上绘制取决于日期(形式为 2018 年 9 月 14 日至 2018 年 12 月)的趋势线。

I've tried using Seaborn:我试过使用 Seaborn:

#dh1018['BILLDATE'] returns a pandas series of strings containing the dates from Sep-14 to Dec-2018.
dh1018=df.loc[107:158,['BILLDATE','Covel']]
dates=dh1018['BILLDATE']

#plotting the actual data
plot(dates, dh1018['Covel'], label='Covel')

#trying to get that trend line
import seaborn as sns
sns.regplot(x=dates, y=dh1018['Covel'], data=dh1018, fit_reg=True)

xlabel('Billdate')
ylabel('Monthly kWh')
title('Monthly kWh of Dining Hall Buildings 2010-2018')
legend(loc='best')
fig_size=rcParams["figure.figsize"]
fig_size[0]=20
fig_size[1]=10
_=plt.xticks(rotation=90) 

In the end, I receive a TypeError basically saying it couldn't convert the dates Sep-14...Dec-18 to numeric.最后,我收到一个 TypeError 基本上说它无法将日期 Sep-14...Dec-18 转换为数字。 So I guess my question boils down to: how do I convert my date format into a number?所以我想我的问题归结为:如何将日期格式转换为数字? All the examples I've found are in neat isoformat.我发现的所有例子都是整齐的isoformat。

Try using pd.to_datetime() :尝试使用pd.to_datetime()

import pandas as pd

dates = pd.to_datetime(dh1018['BILLDATE'])

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

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