【问题标题】:How to Graph Trend Line as function of Dates?如何将趋势线绘制为日期的函数?
【发布时间】:2019-08-23 00:48:16
【问题描述】:

我正在尝试在与我的实际数据值相同的图上绘制取决于日期(以 2018 年 9 月 14 日至 2018 年 12 月的形式)的趋势线。

我尝试过使用 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) 

最后,我收到一个 TypeError,基本上说它无法将日期 Sep-14...Dec-18 转换为数字。所以我想我的问题归结为:如何将日期格式转换为数字?我发现的所有示例都采用整洁的等格式。

【问题讨论】:

    标签: python date trendline best-fit


    【解决方案1】:

    尝试使用pd.to_datetime()

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

    【讨论】:

      猜你喜欢
      • 2016-02-06
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2022-10-14
      • 2018-07-18
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多