mplfinance
mplfinance copied to clipboard
Update plot, remove old data show new data
Thanks for an amazing library!
I'm trying to create an application where the user can select a ticker and then click a button that prints candlesticks for the ticker. I'm plotting the graph inside a frame in Tkinter.
The user can select a ticker in the drop down and Graph the data for the ticker.
I first create the empty graph
def _make_empty_graph(self):
self.fig = mpf.figure()
self.canvas = FigureCanvasTkAgg(self.fig, self.graph_frame)
self.canvas.get_tk_widget().pack()
I then create the graph with data from a dataframe, after the user has selected a ticker and clicked "Graph". I destroy
the children
in the frame to not add another graph under the previous graph. The user can then select yet another ticker and graph it.
def _make_graph(self, ticker_data=None):
self._get_ticker_data()
for widgets in self.graph_frame.winfo_children():
widgets.destroy()
self.fig, _ = mpf.plot(self.ticker_df, type='candle',
title = f'{self.ticker_var.get()}',
volume=True, style='binance', returnfig=True)
self.canvas = FigureCanvasTkAgg(self.fig, self.graph_frame)
self.canvas.get_tk_widget().pack()
Can I solve this a bit more elegant by using subplots or similar and then just clear the graph before I create the new graph? I still want to show an empty graph in the beginning. Do I need to create the empty graph in some other way to be able to update it later? Later on I like to add buttons to be able to RSI, Bollinger bands etc. on top the candlesticks.
Suggestions are greatly appreciated.
I don't know if this will work (and don't have time now to test it myself) but have you tried this: Instead of destroying ... save the axes list:
self.fig, self.axlist = mpf.plot(..., returnfig=True)
Then when you want a black graph:
for ax in self.axlist:
ax.clear()
Please let me know if that works.
If it does work, then in theory you will only need to ever call _make_empty_graph()
one time at the beginning.
Then do the ax.clear()
stuff just before plotting each new ticker.
You also may need to modify _make_graph()
so that the first time it is called it uses returnfig=True
but on subsequent calls it passes in the Axes objects. This is not unlike the animation example here. For more information see also external axes mode.
Thank you very much. i wasn't able to solve it by clearing the the ax
. Seems like I have to use destroy()
when using a canvas
in Tkinter.
@g3blv Thank you for the update. Much appreciated. I don't know much about tkinter, however we do get several mplfinance/tkinter related questions from time to time. I appreciate your taking the time to post here. Thank you.
please assign this task to me ...
@Anshika91
What is it that you are planning to do regarding this issue?