2016-12-06 83 views
0

我有一個數據幀,如遵循蟒蛇大熊貓IPython中matplotlib金融timeserie

     open  high  low close volume 
timestamp              
2000-02-01 00:30:00 1401.00 1401.50 1400.50 1401.25  8 
2000-02-01 01:00:00 1401.50 1401.50 1400.25 1400.25  10 
2000-02-01 01:30:00 1400.25 1401.00 1399.50 1400.75  18 
2000-02-01 02:00:00 1400.50 1401.00 1399.75 1400.00  15 
2000-02-01 02:30:00 1399.75 1399.75 1399.25 1399.25  6 
2000-02-01 03:00:00 1400.00 1400.00 1399.50 1399.50  6 
2000-02-01 03:30:00 1399.25 1399.25 1398.25 1398.25  10 
2000-02-01 04:00:00 1398.50 1399.00 1398.25 1398.75  7 
2000-02-01 04:30:00 1399.00 1400.25 1399.00 1400.00  13 
2000-02-01 05:00:00 1399.75 1400.50 1399.25 1400.25  26 
2000-02-01 05:30:00 1400.00 1400.75 1399.50 1400.50  24 
2000-02-01 06:00:00 1400.00 1400.00 1399.00 1399.25  23 
2000-02-01 06:30:00 1399.50 1404.00 1399.50 1403.50  96 
2000-02-01 07:00:00 1403.50 1405.00 1402.50 1402.50  108 
2000-02-01 07:30:00 1402.50 1404.50 1400.50 1401.00  162 
2000-02-01 08:00:00 1400.75 1402.50 1399.50 1401.25  166 
2000-02-01 08:30:00 1401.25 1403.75 1397.25 1398.25 2009 
2000-02-01 09:00:00 1398.50 1403.75 1391.25 1395.50 2497 
2000-02-01 09:30:00 1395.50 1404.25 1394.75 1400.75 2071 
2000-02-01 10:00:00 1400.75 1404.50 1399.75 1403.00 1528 
2000-02-01 10:30:00 1403.00 1405.25 1399.25 1399.50 1253 
2000-02-01 11:00:00 1399.25 1407.75 1398.25 1407.25 1226 
2000-02-01 11:30:00 1407.00 1409.00 1406.00 1408.75 1079 
2000-02-01 12:00:00 1408.75 1411.50 1408.00 1409.50 1091 
2000-02-01 12:30:00 1409.75 1410.00 1405.00 1406.25 1129 
2000-02-01 13:00:00 1406.25 1412.50 1405.50 1409.50 1361 

我想打印在IPython的筆記本功能matplotlib.finance。

我曾嘗試以下

import matplotlib.finance as mpf 
import matplotlib.pyplot as plt 
fig, ax = plt.subplots(figsize=(8,5)) 
mpf.candlestick_ohlc(ax, data) 

,其中數據是我的數據幀。這是金融書python中給出的例子,然而在這種情況下,數據直接從雅虎網站檢索。我還沒有找到一種重新利用這個例子的方法 - 我對Python很新,我真的不知道該怎麼開始嘗試。所有的幫助表示讚賞。謝謝!

+1

具體是什麼問題? – JohnE

+0

我只需要一個簡單的語法示例,可以使熊貓數據框已經格式化爲ohlc,以蠟燭圖的形式進行繪圖...即時通訊相當丟失,我可以告訴它需要數小時(如果沒有考慮到我只有幾天在我的日常工作之外有幾個小時就可以做到這一點)通過瀏覽網頁來找出問題,所以如果有人已經有了這樣的知識分享,那對我會有很大的幫助。 –

回答

0

matplotlib.finance已經擁有了雅虎的讀者,將帶回的數據以適當的格式:

%matplotlib inline 
import matplotlib.pyplot as plt 
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc 

fig, ax = plt.subplots() 
quotes = quotes_historical_yahoo_ohlc('GOOG', (2016, 11, 1), (2016, 11, 30)) 
candlestick_ohlc(ax, quotes, width=0.5) 
plt.show() 

您將需要實際更新軸呈現標籤的日期。

pandas數據幀轉換。您的dataframepandas.datareader的默認值之間的唯一區別是您的索引(timestampDate)的標籤,因此只需在下面的示例中重新命名即可。您將需要reset_index和重新排列(reindex)的列到OCHL格式:

import pandas_datareader.data as web 

data = web.get_data_yahoo('AMZN', '11/1/2016', '12/1/2016') 
quotes = data.reset_index().reindex_axis(['Date','Open','Close','High','Low'], axis=1) 
quotes['Date'] = quotes['Date'].astype(int) 
fig, ax = plt.subplots() 
candlestick_ohlc(ax, quotes.values, width=0.5) 
plt.show() 

但需要修復的x軸。

+0

是的,我知道這個例子。 我不想從雅虎檢索數據 - 我想繪製一個熊貓數據框,如上面顯示的那個(我已經保存爲描述完整性的.h5),並且我不知道如何重新調整您的代碼片段正在提供我的具體情況。 –