我正在试图建立一个图表,显示一个数据种子烛台。下面提供了代码。这个图表应该是动画的,但它不会更新。任何一个人都能帮忙吗?
class Graph(object):
def __init__(self, ticker_name):
self.datafeed = self.get_datafeed(ticker_name)
self.figure = plt.figure()
self.ax1 = self.figure.add_subplot(211)
self.ax2 = self.figure.add_subplot(212)
animation.FuncAnimation(self.figure, self.animate, interval=10000, init_func=self.init_figure)
plt.show()
def get_datafeed(self, ticker_name):
self.parameters = {"ticker_name": ticker_name,
"history": 100}
return DataFeed(self.parameters)
def init_figure(self):
self.ax1.xaxis.set_major_formatter(DateFormatter('%H:%M'))
self.ax1.grid()
self.ax2.grid()
self.ax1.set_title(self.parameters["ticker_name"].upper())
def animate(self, i):
time.sleep(2)
self.datafeed.refresh()
data = self.datafeed.query(10)
self.ax1.clear()
self.ax2.clear()
self.plot_candles(data[["time", "open", "high", "low", "close"]])
def plot_candles(self, df):
df.loc[:, "time"] = df.time.apply(date2num)
mpf.candlestick_ohlc(self.ax1, df.values.tolist(), width=0.0005, colordown="r", colorup="g")
self.ax1.set_ylim(df["low"].min() - 5, df["high"].max() + 5)发布于 2017-07-04 16:20:27
不要在动画中使用time.sleep()。相反,使用interval参数FuncAnimation来控制更新速度。
您还需要保留对FuncAnimation的引用。使用
self.ani = animation.FuncAnimation( ... )否则,图一显示,FuncAnimation就会被垃圾收集。
(请注意,如果这不能解决问题,则需要提供
https://stackoverflow.com/questions/44909330
复制相似问题