2016-04-14 65 views
1
import os 
from matplotlib.backends.backend_pdf import PdfPages 
import pandas as pd 
import matplotlib.pyplot as plt 
import datetime as dt 

pp = PdfPages('multipage.pdf') 

pth = "D:/Technical_Data/" 
for fle in os.listdir(pth): 
    df = pd.read_csv(os.path.join(pth, fle),usecols=(0, 4)) 
    if not df.empty: 
     df=df.astype(float) 
     days = df['indx'] 
     value = df['Close'] 
     plt.plot_date(x=days, y=value,fmt="r-") 
     plt.title(fle) 
     plt.ylabel("Price") 
     plt.grid(True) 
     pp.savefig() 
pp.close() 

我正在遍歷目錄中的文件,並將所有圖形保存爲pdf文件。日期格式爲20150101ValueError:年份超出範圍pyplot

但它引發錯誤:

ValueError: year is out of range

的樣本數據

indx open High Low Close Volume 
20140103 31.9823 32.1511 31.8382 32.1213 2034100 
20140103 5.28 5.29 5.26 5.27 10387300 
20140103 33.9 34.03 33.77 34 930800 
20140103 10.62 10.63 10.51 10.6 2004500 
20140103 3.42 3.49 3.42 3.49 3837600 
20140103 1.69 1.71 1.685 1.705 6870300 
20140103 42.5 43.61 42.3 43.47 255500 
+0

很難調試代碼,而無需輸入例。請打印'days'變量的值併發布,我懷疑問題出在那裏。 –

+0

@TonyBabarino有問題 – nnnnmmm

回答

0

你需要轉換DF [ 'INDX']到大熊貓DatetimeIndex

# df=df.astype(float) # do not covert yymmdd to float 
days = pd.to_datetime(df['indx'].astype(str), format='%Y%m%d') 
plt.plot_date(x=days, y=value, fmt="r-") 
0

問題是天數列表格式。您必須將這些值轉換爲DateTime類型或浮動,表示自0001-01-01 UTC以來的天數。

從matplotlib.pyplot documentation

plot_date(x, y, fmt='bo', tz=None, xdate=True, ydate=False, **kwargs)

x and/or y can be a sequence of dates represented as float days since 0001-01-01 UTC.