Python: Embed pandas plot in Tkinter GUI
pandas
uses matplotlib
for plotting. Most pandas
plotting functionality takes an ax
kwarg that specifies the axes object that will be used. There are a few pandas
functions that can't be used this way, and will always create their own figure/axes using pyplot
. (e.g. scatter_matrix
)
For a simple case based on your example, however:
import matplotlibimport numpy as npfrom matplotlib.backends.backend_tkagg import FigureCanvasTkAggfrom matplotlib.figure import Figureimport pandas as pdimport Tkinter as tkimport ttkroot = tk.Tk()lf = ttk.Labelframe(root, text='Plot Area')lf.grid(row=0, column=0, sticky='nwes', padx=3, pady=3)t = np.arange(0.0,3.0,0.01)df = pd.DataFrame({'t':t, 's':np.sin(2*np.pi*t)})fig = Figure(figsize=(5,4), dpi=100)ax = fig.add_subplot(111)df.plot(x='t', y='s', ax=ax)canvas = FigureCanvasTkAgg(fig, master=lf)canvas.show()canvas.get_tk_widget().grid(row=0, column=0)root.mainloop()