-1

The code is giving me and error saying that there is no image. Any suggestions helpful It is saying that the image1 is not an image, which as you can see is not true as it is created when the code is run.

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()

Jerseytbw
  • 1
  • 1
  • 4
    Please update your question with the full error traceback. – quamrana Mar 05 '23 at 18:15
  • I'm not familiar with Seaborn - did that call to `.pairplot()` result in a window that displays the plot, prior to your attempt at loading the image into your own window? If so, that original window would have created an instance of `Tk()`, which becomes the default home for created objects such as images. Such objects simply won't work with other instances of `Tk()`, such as your `root`. If this is the case, the solution is to add a `master=root` parameter to the call to `PhotoImage()`, so that the image gets created in the right instance. – jasonharper Mar 05 '23 at 20:36
  • Add `import matplotlib` and `matplotlib.use('agg')`. – acw1668 Mar 06 '23 at 15:32

1 Answers1

0

you can use variable and then save:

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()