How can I integrate Seaborn plot into Tkinter GUI

孤街醉人 提交于 2019-12-11 16:39:38

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/55680827/how-can-i-integrate-seaborn-plot-into-tkinter-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!