如何在tkinter中使用pandas绘制数据框条形图?

时间:2017-03-29 20:39:30

标签: pandas matplotlib dataframe graph tkinter

我试图在tkinter中绘制一个数据帧图,以便在应用程序的窗口中绘制它。

我的代码是:

import tkinter as tk
from tkinter import ttk, Canvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import pandas as pd

class application():

    def __init__(self):
        self.win = tk.Tk()
        self.win.title('Emetor')
        self.win.geometry('700x650+230+30')
        self.win.configure(background='pale green')
        self.createWidgets()

    def createWidgets(self):
        self.var1 = tk.IntVar()
        self.check1 = tk.Checkbutton(self.win, text='1',command= self.mat, variable=self.var1)
        self.check1.grid(row=1,column=0)

    def mat(self):
        data=[{'a':1,'b':2},{'a':1,'b':4},{'a':1,'b':6},{'a':1,'b':8}]
        index=['1','2','3','4']

        fig=Figure(figsize=(4,5), facecolor='white')        
        a=fig.add_subplot(111)

        df = pd.DataFrame(data, index=index)
        df.plot(kind='bar')
        plt.show()

        canvas=FigureCanvasTkAgg(fig, self.win)
        canvas.get_tk_widget().grid(row=1, column=0, columnspan=4)
        canvas.show()

if __name__ == '__main__':
    app = application()
    app.win.mainloop()

结果是: dataframe bar graph

如果有人知道如何让主寡妇的人物展示不是分开的,那对我来说将是一个很大的帮助。

谢谢:)

0 个答案:

没有答案