[英]Plotting graph using matplotlib in Jupyter iPython Notebook
[英]Candlestick charts in matplotlib - Jupyter notebook is not plotting
我一直在開發一個交易機器人,在此過程中,我嘗試使用matplotlib制作實時燭台圖。 問題是每次我運行Jupyter筆記本(或Spyder中的iPython)時,我的圖都不會顯示或出現
([<matplotlib.lines.Line2D at 0x113bee978>,
<matplotlib.lines.Line2D at 0x1131204a8>,
<matplotlib.lines.Line2D at 0x113095f28>,
<matplotlib.lines.Line2D at 0x1131c3fd0>,
<matplotlib.lines.Line2D at 0x1132ef160>,
<matplotlib.lines.Line2D at 0x1132efa20>],
[<matplotlib.patches.Rectangle at 0x11312f978>,
<matplotlib.patches.Rectangle at 0x113120278>,
<matplotlib.patches.Rectangle at 0x113061358>,
<matplotlib.patches.Rectangle at 0x1131eafd0>,
<matplotlib.patches.Rectangle at 0x1132ef390>,
<matplotlib.patches.Rectangle at 0x1132effd0>])
我嘗試使用%matplotlib內聯和plt.show(),似乎沒有任何效果。 在這里,它遵循我正在使用的代碼。
***順便說一句,我正在使用Mac計算機和Anaconda的Jupyter
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
from matplotlib.dates import date2num
from matplotlib.finance import candlestick_ohlc
import numpy as np
import requests
import time
import pandas as pd
import datetime as dt
def getNow(pair):
return requests.get('https://poloniex.com/public?command=returnTicker').json()[pair]
def getPast(pair, period, daysBack, daysData):
now = int(time.time())
end = now-(24*60*60*daysBack)
start = end-(24*60*60*daysData)
base = 'https://poloniex.com/public?command=returnChartData¤cyPair='
response = requests.get('{0}{1}&start={2}&end={3}&period={4}'.format(base, pair, start, end, period))
return response.json()
pair = "USDT_BTC" # Use ETH pricing data on the BTC market
period = 7200 # Use 7200 second candles
daysBack = 0 # Grab data starting 0 days ago
daysData = 15 # From there collect 15 days of data
# Request data from Poloniex
data = getPast(pair, period, daysBack, daysData)
# Convert to Pandas dataframe with datetime format
data = pd.DataFrame(data)
#Convert dates do float for matplotlib
data.date = data.date.astype(float)
#Define ohlc
date, closes, highs, lows, opens, volume = data['date'], data['close'], data['high'],data['low'], data['open'], data['volume']
ohlc = [date, opens, highs, lows, closes, volume]
#Plot with three regions
fig = plt.figure(facecolor='#f0f0f0')
ax1 = plt.subplot2grid((6,1), (0,0), rowspan=1, colspan=1)
plt.title(pair)
plt.ylabel('H-L')
ax2 = plt.subplot2grid((6,1), (1,0), rowspan=4, colspan=1, sharex=ax1)
ax2v = ax2.twinx()
plt.ylabel('Price')
ax3 = plt.subplot2grid((6,1), (5,0), rowspan=1, colspan=1, sharex=ax1)
plt.ylabel('MAvgs')
#Candlestick plot
candlestick_ohlc(ax1, ohlc, colorup='#77d879', colordown='#db3f3f')
plt.show()
看來函數中插入的數據格式錯誤。 在這種情況下,這是定義ohlc的正確方法:
date, closes, highs, lows, opens, volume = data['date'].tolist(), data['close'].tolist(), data['high'].tolist(),data['low'].tolist(), data['open'].tolist(), data['volume'].tolist()
ohlc = [tuple([date[i],opens[i],highs[i],lows[i], closes[i]
]) for i in range(len(date))]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.