Ich versuche, ein Diagramm in matplotlib
zu generieren, speichern Sie es in ein Bild im Speicher und dann das Bild auf eine tkinter.Canvas
.Speichern matplotlib Diagramm in den Speicher und Platzierung auf tkinter Leinwand
Mein Code ist wie folgt:
%matplotlib inline
import io
from PIL import Image
import matplotlib.pyplot as plt
import tkinter
gr_data = ([1, 2])
plt.figure()
plt.plot(gr_data)
plt.title("test")
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
im = Image.open(buf)
window = tkinter.Tk()
window.title('Test')
window.geometry('500x500-200+100')
photo = tkinter.PhotoImage(file=im)
can = tkinter.Canvas(window, height=480, width=600, bg='light blue')
can.pack()
can.create_image(0,0, image=photo, anchor='nw')
buf.close()
window.mainloop()
und ich erhalte den folgenden Fehler:
---------------------------------------------------------------------------
TclError Traceback (most recent call last)
<ipython-input-15-484d38f49c92> in <module>()
19 window.geometry('500x500-200+100')
20
---> 21 photo = tkinter.PhotoImage(file=im)
22
23 can = tkinter.Canvas(window, height=480, width=600, bg='light blue')
C:\Users\Kevin\dev.local\python\Anaconda3\lib\tkinter\__init__.py in __init__(self, name, cnf, master, **kw)
3401 Valid resource names: data, format, file, gamma, height, palette,
3402 width."""
-> 3403 Image.__init__(self, 'photo', name, cnf, master, **kw)
3404 def blank(self):
3405 """Display a transparent image."""
C:\Users\Kevin\dev.local\python\Anaconda3\lib\tkinter\__init__.py in __init__(self, imgtype, name, cnf, master, **kw)
3357 v = self._register(v)
3358 options = options + ('-'+k, v)
-> 3359 self.tk.call(('image', 'create', imgtype, name,) + options)
3360 self.name = name
3361 def __str__(self): return self.name
TclError: couldn't open "<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=432x288 at 0x294D8E70E80>": no such file or directory
alle anderen Posting ich gefunden habe, scheinen zu zeigen, dass das, was ich tue, richtig ist, aber Es funktioniert nicht, irgendwelche Ideen? Ich bin in Python 3.
Das tat es, danke! –