I am trying to plot using Seaborn in Tkinter. My approaches so far were different variations of this and I could not get it to work.
I tried the matplotlib.use("Agg"), which works fine one the normal Matplotlib graphs on the page but doesn't seem to work on the Seaborn plots
matplotlib.use("TkAgg") # 'Agg' doesnt work either
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import seaborn as sns
import tkinter as tk
def graphspage():
pf = tk.Tk()
pf.geometry("1000x800")
### Works
f = Figure(figsize=(5, 5), dpi=100)
a = f.add_subplot(111)
a.plot(df['date'],daily_drawdown, 'b-',df['date'], daily_drawdownbm, 'k-', linewidth=1)
f.tight_layout()
canvas = FigureCanvasTkAgg(f,pf)
canvas.get_tk_widget().grid(row=1,column=1)
### Doesnt Work
pct = diststats()[4]
pctbm = diststats()[5]
f = Figure(figsize=(5, 5), dpi=100)
a = f.add_subplot(111)
a.sns.distplot(pct,label = 'Portfolio')
a.sns.distplot(pctbm,axlabel='Distribution of returns',label='Benchmark')
canvas = FigureCanvasTkAgg(f,pf)
canvas.get_tk_widget().grid(row=2,column=1)
graphspage()
I took example from Seaborn page - so I can run it - and I put in tkinter
Code in
create_plot
usesmatplotlib
withseaborn
to create plot and it returnsfigure
.Later this code adds
figure
to tkinter's windowExample doesn't need
matplotlib.use('TkAgg')
normatplotlib.use('Agg')
There are
tkinter.Label
, Seaborn's plot andtkinter.Button
but I have also example withNavigationToolbar2Tk
Full code: