我一直在开发一个交易机器人,在此过程中,我正在尝试使用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()
答案 0 :(得分:0)
看起来插入函数的数据格式错误。在这种情况下,这是定义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))]