Skip to content
Advertisement

How to add histogram from dataframe in tkinter

I am new to Tkinter and am working on a GUI based on ML. I want to add a histogram plot from a dataframe into Tkinter and am stuck. This is the histogram plot:

enter image description here

This is part of my code

class Hplot:
    def __init__(self,data,master):
        self.master = master
        self.data = data

        self.window = Toplevel(self.master)
        self.window.title("Histogram Plot")
        self.window.configure(background='white')
        #self.window.resizable(False, False)

        self.figure = Figure(figsize=(5, 5), dpi=100)
        self.sub = self.figure.add_subplot(111)
        self.sub.hist(data,bins=50)
        self.sub.plot()

        self.canvas = FigureCanvasTkAgg(self.figure, master=self.window)
        self.canvas.get_tk_widget().pack()
        #self.canvas.draw()

Pls suggest the correction.

Advertisement

Answer

You can save the histogram to an image and then open it and display it in tkinter.
You can save your matplotlib image by using this:

import matplotlib.pyplot as plt

plt.savefig('histogram.png')

And if you face any problem opening and displaying the image on tkinter, that is answered here on SO

Advertisement