添加未来日期以绘制trendlin

2024-10-02 02:32:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我用matplotlib绘制了一些csv数据。我还将趋势线(线性拟合)放在数据顶部。我想扩展日期范围,这样我的趋势线就可以对未来6个月的数据进行预测。在

我一整天都在敲键盘。在

csv数据是

Date,Cash Bucks
29/07/2015,4010.14
22/08/2015,4471.09
26/08/2015,4685.6

我得到的不能预测未来的代码是

^{pr2}$

如何增加日期范围和趋势线图来预测未来?在


Tags: csv数据代码datematplotlib绘制线性cash
1条回答
网友
1楼 · 发布于 2024-10-02 02:32:05

在绘制实际数据之后,需要将end_date附加到x1之后,在绘制趋势线之前,用新的附加值重新生成{}。在

所以,脚本的结尾将是:

# Plot
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1, axisbg='white')

# Plot actual data
plt.plot_date(x=x1, y=y1, fmt='o-')

# Now append the extra data
x1.append(date_end)
x2 = mdates.date2num(x1)

plt.plot(x1,p(x2),'r ') #add trendline to plot

plt.title('Cash Bucks')
plt.ylabel('Cash Bucks')
plt.xlabel('Date')

fig.autofmt_xdate() # This tidies up the x axis
plt.show()

我还为您添加了fig.autofmt_xdate(),这使得x轴的标签更好一些

enter image description here

相关问题 更多 >

    热门问题