2015-11-05 116 views
2

我有一些csv数据,我用matplotlib绘制。我还把一个趋势线(线性拟合)放在数据的顶部。我想延长日期范围,以便我的趋势线能够预测未来6个月的数据。添加未来的日期绘制趋势线

我一直在键盘上敲我的头一整天。

CSV数据是

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

而且我已经得到了代码不预测未来是

import csv 
from datetime import datetime, timedelta 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 
import pandas as pd 

filename = "statistics.csv" 
f=open(filename, "rb") 
reader = csv.reader(f) 
headers = reader.next() 

converters = [str.strip] + [float] * (len(headers) - 1) # get numeric values into floats 

column = {} 
for h in headers: 
    column[h] = [] 

for row in reader: 
    for h, v, conv in zip(headers, row, converters): 
     column[h].append(conv(v)) 

dates_list = [datetime.strptime(date, '%d/%m/%Y').date() for date in column['Date']] 

f.close() 

date_start = dates_list[0] 
date_end = dates_list[-1] + timedelta(3*365/12) 
# dates_list.append(date_end) 

print dates_list 
x1 = dates_list 
x2 = mdates.date2num(x1) 
y1 = column['Cash Bucks'] 

z=np.polyfit(x2,y1,1) 
p=np.poly1d(z) 

# 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-') 
plt.plot(x1,p(x2),'r--') #add trendline to plot 

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

如何提高日期范围和趋势线的情节看未来?

回答

2

在绘制实际数据之后,您需要将end_date附加到x1,然后在绘制趋势线之前用新附加值重新制作x2

所以,你的脚本结束看起来像:

# 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