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()
The OP's original code only lacked a
canvas.draw()
, if I'm not mistaken. This has also been indicated by furas. I recently found it difficult to find a full example of how to draw with Seaborn on a Tkinter GUI and especially, how to redraw on the canvas.So, let me give you a fully working but mininmal snippet for a program that initially draws and redraws on every keypress.
I used some example code provided by matplotlib.
This is how the code above looks when executed and when pressing some keys: