python 为什么当我尝试将PNG图像输出到tkinter GUI时会出现错误?

6uxekuva  于 2023-03-06  发布在  Python
关注(0)|答案(1)|浏览(129)

代码给我一个错误,说没有图像,有什么有用的建议吗?它说image1不是图像,正如你所看到的,这是不正确的,因为它是在代码运行时创建的。

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from tkinter import *
from PIL import ImageTk, Image 
import os

weatherdata = pd.read_csv('london_weather.csv')

#graph the data

sns.pairplot(weatherdata,
             x_vars="pressure",
             y_vars="precipitation",
             hue="mean_temp",
             aspect=3,
             height=3)
plt.savefig('image1.png')

#All the code above is working however i cannot output the image, which is really annoying.
root = Tk()
img = ImageTk.PhotoImage(Image.open("image1.png"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
5f0d552i

5f0d552i1#

您可以使用变量,然后保存:

fig=plt.figure()
p = sns.pairplot(weatherdata, x_vars="pressure", y_vars="precipitation",
                 hue="mean_temp",
                 aspect=3,
                 height=3)
p.savefig("image1.png", facecolor='w') 

root = Tk()
img = ImageTk.PhotoImage(Image.open("image1.png"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()

相关问题