How can I integrate Seaborn plot into Tkinter GUI

10.7k views Asked by At

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()
2

There are 2 answers

1
Manu CJ On BEST ANSWER

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.

import tkinter
from typing import Callable

import numpy as np
import seaborn as sns
from matplotlib.backend_bases import key_press_handler
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure


def init_gui(root, update_function: Callable) -> FigureCanvasTkAgg:
    def event_key_press(event):
        print("you pressed {}".format(event.key))
        update_function()
        key_press_handler(event, canvas)

    # create empty figure and draw
    init_figure = create_figure()
    canvas = FigureCanvasTkAgg(init_figure, master=root)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
    # call key press event
    canvas.mpl_connect("key_press_event", event_key_press)
    return canvas


def create_figure() -> Figure:
    # generate some data
    matrix = np.random.randint(20, size=(10, 10))
    # plot the data
    figure = Figure(figsize=(6, 6))
    ax = figure.subplots()
    sns.heatmap(matrix, square=True, cbar=False, ax=ax)
    return figure


def redraw_figure():
    figure = create_figure()
    canvas.figure = figure
    canvas.draw()


sns.set()
root = tkinter.Tk()
canvas = init_gui(root, update_function=redraw_figure)

tkinter.mainloop()

I used some example code provided by matplotlib.

This is how the code above looks when executed and when pressing some keys:

Preview of Tkinter with Seaborn plot

0
furas On

I took example from Seaborn page - so I can run it - and I put in tkinter

Code in create_plot uses matplotlib with seaborn to create plot and it returns figure.
Later this code adds figure to tkinter's window

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack()

Example doesn't need matplotlib.use('TkAgg') nor matplotlib.use('Agg')

There are tkinter.Label, Seaborn's plot and tkinter.Button but I have also example with NavigationToolbar2Tk

Full code:

# seaborn in matplotlib - tkinter doesn't need it
#import matplotlib
#matplotlib.use('TkAgg')

# embed matplotlib in tkinter 
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

# seaborn example
from string import ascii_letters
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

def create_plot():
    sns.set(style="white")

    # Generate a large random dataset
    rs = np.random.RandomState(33)
    d = pd.DataFrame(data=rs.normal(size=(100, 26)),
                     columns=list(ascii_letters[26:]))

    # Compute the correlation matrix
    corr = d.corr()

    # Generate a mask for the upper triangle
    mask = np.zeros_like(corr, dtype=np.bool)
    mask[np.triu_indices_from(mask)] = True

    # Set up the matplotlib figure
    f, ax = plt.subplots(figsize=(11, 9))

    # Generate a custom diverging colormap
    cmap = sns.diverging_palette(220, 10, as_cmap=True)

    # Draw the heatmap with the mask and correct aspect ratio
    sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
                square=True, linewidths=.5, cbar_kws={"shrink": .5})

    return f

# --- main ---

root = tkinter.Tk()
root.wm_title("Embedding in Tk")

label = tkinter.Label(root, text="Matplotlib with Seaborn in Tkinter")
label.pack()

fig = create_plot()

canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack()

button = tkinter.Button(root, text="Quit", command=root.destroy)
button.pack()

tkinter.mainloop()

enter image description here