0

I am writing ACP code, and in my code I have many graphs (from matplotlib) which I don't want to show just saved, expect one which has to be in the tkinter interface. However, this doesn't work.

enter image description here

I have tried to saved it in PNG. Then, for tkinter, it should have been easy since I just have to show an image. However, if I run my code, it shows just a big grey square. I have tried FigureCanvasTkAgg, which works, but the graph doesn't have the label for every point, the grill, the title....

def ACP():
.....

    df = pd.DataFrame({
    'x': x,
    'y':VaPtri,
    'group': Variance
    })
    sns.regplot(data=df, x="x", y="y", fit_reg=False, marker="+", color="skyblue")
    # basic plot
    p1=sns.regplot(data=df, x="x", y="y", fit_reg=False, marker="o", color="skyblue")
    # add annotations one by one with a loop
    for line in range(0,df.shape[0]):
        p1.text(df.x[line]+0.1, df.y[line], df.group[line], horizontalalignment='left', size='medium', color='black', weight='semibold')
    plt.grid()
    plt.xlabel('Numero de valeur propre')
    plt.ylabel('Valeur propre')
    plt.title('Variance accumulé')
    plt.savefig(nom+'variance'+'.png')
    plt.plot(x,VaPtri)

    image = Image.open(nom+'variance'+'.png')
    photo = ImageTk.PhotoImage(image)
    largeur = photo.width()  # dimensions en nombre de pixels
    hauteur = photo.height()
    zone_image = Canvas(Mafenetre, width = largeur, height = hauteur) # crée un canevas de dimensions ajustées à celles de l'image
    zone_image.create_image(0,0, anchor = NW, image = photo) # association image/widget
    zone_image.grid(column=0,row=10) # placement du widget
    return 
def graphique():....return
##interface
Mafenetre = Tk()
Mafenetre.title("ACP")
Mafenetre.geometry('1200x600')
Mafenetre.configure(bg = "white")

#bouton lancer Graphique
Frame12 = Frame(Mafenetre,borderwidth=2,relief=GROOVE,bg='gold')
Frame12.grid(rowspan=2,column=  2,row=6)
Label(Frame12,text=" Projections",bg='gold').grid(row=6, column=2)
Button(Frame12,text="Lancer",fg='navy',command=Graphique,bg='gold').grid(row=7, column=2)
#bouton lancer ACP
Frame2 = Frame(Mafenetre,borderwidth=2,relief=GROOVE,bg='deep sky blue')
Frame2.grid(rowspan=2,column=2,row=2)
Label(Frame2,text="ACP",bg='deep sky blue').grid(row=2, column=2)
Button(Frame2,text="Lancer",fg='navy',command=ACP,bg='deep sky blue').grid(row=3, column=2)

And this is not a format issue. Indeed, sometimes the graph works but in very special moments. First when the graph is in the second function. Second when I don't put the plt.show() away (but I need them to be remove so there's no new popup with the graph.) If needed I can show more code or the graph or the interface.

aschultz
  • 1,658
  • 3
  • 20
  • 30
Simon
  • 1
  • see this: https://stackoverflow.com/questions/25498937/embed-a-pyplot-in-a-tkinter-window-and-update-it – Zaraki Kenpachi Aug 06 '19 at 08:11
  • If you are asking about why the image doesn't show up, read [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – Henry Yik Aug 06 '19 at 08:15
  • THANKS, by puting photo and image as global it work now. thanks again – Simon Aug 06 '19 at 13:50

1 Answers1

0

I'll show how I do it with my application. But basically you will create a figure, subplot your data and put it into a FigureCanvasTkAgg, and then place it in your tk window, like this:

from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

fb = Figure(figsize=(5, 2.5), dpi=100)
b = fb.add_subplot(111)
b.set_title('Ethernet Packet')
canvas = FigureCanvasTkAgg(fb, master=Root)
canvas.get_tk_widget().place(x=540, y=560)
b.plot(final_data)
Izalion
  • 708
  • 4
  • 11