I hope you are doing well. I am writing a python program at the moment, and I have an extremely surprising and incomprehensible problem. The program being complex, I recreated the problematic part. So I created a simple window in which I put a canvas :
canvas = Canvas(main_window, width=1000, height=500)
canvas.grid(row=1, column=0)
image = Image.new('RGB', (900, 400), color='green')
graph = ImageTk.PhotoImage(image)
img = canvas.create_image(0, 0, anchor=NW, image=graph)
canvas.itemconfigure(img, image=graph)
I also added a simple button which points to a function allowing to create a graph using PyPlot :
def process_graph():
plot_fig("Title", "Label", [0, 1, 2, 3], [10, 100, 1000, 10000], 100, 9, 4, False, False)
image_graph = Image.open("graphnew.jpg")
graph1 = ImageTk.PhotoImage(image_graph)
img_graph = canvas.create_image(0, 0, anchor=NW, image=graph1)
canvas.itemconfigure(img_graph, image=graph1)
return
And here is the plot_fig function:
def plot_fig(title, lab, x_data, y_data, res=100, width=9.4, height=4.6, x_log=False, y_log=False):
plt.close('all')
plt.figure(figsize=(width, height), dpi=res)
axes = plt.gca()
plt.plot(x_data, y_data, label=lab)
if x_log:
plt.xscale('log')
else:
plt.xscale('linear')
if y_log:
plt.yscale('log')
else:
plt.yscale('linear')
plt.title(title)
plt.xlabel("Duration [days]")
plt.legend()
plt.savefig("graphnew.jpg", quality=95)
plt.close('all')
return
The really strange behavior starts here: look at the two parameters x_log and y_log, which allow you to change the axes. All combinations work wonderfully, except one (x_log = False and y_log = False). in all other cases, the program works wonderfully, but does it not refresh the figure ?! Is there something I don't understand? (I should specify that the image is well created! but the canvas freeze three times before becoming green again). All others combinations works well. Here is the full code if needed, I would really appreciate your help, thank's very much! Matthieu
import tkinter as tk
from tkinter.filedialog import *
import matplotlib.pyplot as plt
from PIL import Image, ImageTk
main_window = tk.Tk()
def process_graph():
plot_fig("Title", "Label", [0, 1, 2, 3], [10, 100, 1000, 10000], 100, 9, 4, False, False)
image_graph = Image.open("graphnew.jpg")
graph1 = ImageTk.PhotoImage(image_graph)
img_graph = canvas.create_image(0, 0, anchor=NW, image=graph1)
canvas.itemconfigure(img_graph, image=graph1)
return
def plot_fig(title, lab, x_data, y_data, res=100, width=9.4, height=4.6, x_log=False, y_log=False):
plt.close('all')
plt.figure(figsize=(width, height), dpi=res)
axes = plt.gca()
plt.plot(x_data, y_data, label=lab)
if x_log:
plt.xscale('log')
else:
plt.xscale('linear')
if y_log:
plt.yscale('log')
else:
plt.yscale('linear')
plt.title(title)
plt.xlabel("Duration [days]")
plt.legend()
plt.savefig("graphnew.jpg", quality=95)
plt.close('all')
return
button_graph_process = Button(main_window, text="Process", command=process_graph)
button_graph_process.grid(row=0, column=0)
canvas = Canvas(main_window, width=1000, height=500)
canvas.grid(row=1, column=0)
image = Image.new('RGB', (900, 400), color='green')
graph = ImageTk.PhotoImage(image)
img = canvas.create_image(0, 0, anchor=NW, image=graph)
canvas.itemconfigure(img, image=graph)
main_window.mainloop()