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.
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.