matplotlib / mplfinance

Financial Markets Data Visualization using Matplotlib
https://pypi.org/project/mplfinance/
Other
3.59k stars 621 forks source link

how can I embed my mplfinance graph(mpf_animation_demo1.py) in Tkinter ? #308

Open hmhjapan opened 3 years ago

hmhjapan commented 3 years ago

Ask anything you want about mplfinance usage, project philosophy and/or priorities, or anything else related to mplfinance. Display the following animation graph on the tkinter,environment:python3.6 +windows10 desktop Below is mpf_animation_demo1.py:

import pandas as pd
import mplfinance as mpf
import matplotlib.animation as animation

idf = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True)
idf.shape
idf.head(3)
idf.tail(3)
df = idf.loc['2011-07-01':'2011-12-30',:]

fig = mpf.figure(style='charles',figsize=(7,8))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(3,1,3)

def animate(ival):
    if (20+ival) > len(df):
        print('no more data to plot')
        ani.event_source.interval *= 3
        if ani.event_source.interval > 12000:
            exit()
        return
    data = df.iloc[0:(20+ival)]
    ax1.clear()
    ax2.clear()
    mpf.plot(data,ax=ax1,volume=ax2,type='candle')

ani = animation.FuncAnimation(fig, animate, interval=250)

mpf.show()
hmhjapan commented 3 years ago
import tkinter
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

import numpy as np

root = tkinter.Tk()
root.wm_title("Embedding in Tk")
fig = Figure(figsize=(5, 4), dpi=100)

t = np.arange(0, 3, .01)

years =[1950,1960,1970,1980,1990,2000,2010]
gdp=[300.2,543.3,1075.9,2862.5,5979.6,10289.7,14958.3]
fig.add_subplot(111).plot(years,gdp,color='red',marker='o')

canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas, toolbar)

canvas.mpl_connect("key_press_event", on_key_press)

def _quit():
    root.quit()
    root.destroy()

button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)

tkinter.mainloop()

Above is the source code of tkinter ,How to take and assemble two source code files? thank you!

DanielGoldfarb commented 3 years ago

I am sorry I am not much of an expert in TkInter. Have only played with it once or twice.

There is an example you can look at here: https://github.com/matplotlib/mplfinance/wiki/TkInter-Example

And some additional sample code here: https://github.com/matplotlib/mplfinance/issues/304

I would like to have a one or two good mplfinance tkinter demos to post here in the repository, but for me to do it myself I would probably have to spend a full couple of days learning tkinter and creating the demos, and I just don't have that time presently. Hopefully someone reading this who is well versed in tkinter will decide to help our and contribute an example or two to this repository.

Please let me know if the above links help out. All the best. --Daniel