개발자 모드/파이썬(python)

파이썬 python tkinter matplotlib 예제

인생은직구 2022. 8. 7. 12:18
728x90
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk

#-----------------------------------------------------------------
fig = Figure(figsize = (12,8), facecolor ="white")
#-----------------------------------------------------------------
# axis = fig.add_subplot(111) # 1 row, 1 column, only graph
axis = fig.add_subplot(211)   # 2 row, 1 column, Top graph
#-----------------------------------------------------------------

x_value = [1, 2, 3, 4]
y_value = [5, 7, 6, 8]

axis.plot(x_value, y_value)

axis.set_xlabel("Horizontal Label")
axis.set_ylabel("Vertical Label")

# axis.grid()
axis.grid(linestyle = "-")

def _destroyWindows() :
    root.quit()
    root.destroy()
    

root = tk.Tk()    
root.protocol("WM_DELETE_WINDOW", _destroyWindows)

cavans = FigureCanvasTkAgg(fig, master = root)
cavans._tkcanvas.pack(side = tk.TOP, fill = tk.BOTH, expand=1)

root.mainloop()

 

 

728x90