Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Tkinter에서 Matplotlib를 어떻게 실행할 수 있습니까?

<시간/>

Python Matplotlib 라이브러리는 주어진 데이터와 정보를 그래프와 플롯으로 시각화하는 많은 응용 프로그램에서 유용합니다. Tkinter 응용 프로그램에서 matplotlib를 실행할 수 있습니다. 일반적으로 애플리케이션에서 명시적으로 Python 라이브러리를 가져오면 라이브러리의 모든 함수와 모듈에 액세스할 수 있습니다.

matplotlib와 그 기능을 사용하는 GUI 응용 프로그램을 만들려면 matplotlib.pyplot에서 plt로 명령을 사용하여 라이브러리를 가져와야 합니다. . 그러나 Tkagg도 사용합니다. Tkinter 사용자 인터페이스를 대화식으로 사용하는 백엔드에서.

예시

이 예에서는 Tkagg를 가져왔습니다. 및 matplotlib 캔버스 위젯 내부에 플롯하여 주어진 데이터 포인트를 시각화합니다.

# Import required libraries
from tkinter import *
from tkinter import ttk
import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

# Create an instance of tkinter frame
win= Tk()

# Set the window size
win.geometry("700x350")

# Use TkAgg
matplotlib.use("TkAgg")

# Create a figure of specific size
figure = Figure(figsize=(3, 3), dpi=100)

# Define the points for plotting the figure
plot = figure.add_subplot(1, 1, 1)
plot.plot(0.5, 0.3, color="blue", marker="o", linestyle="")

# Define Data points for x and y axis
x = [0.2,0.5,0.8,1.0 ]
y = [ 1.0, 1.2, 1.3,1.4]
plot.plot(x, y, color="red", marker="x", linestyle="")

# Add a canvas widget to associate the figure with canvas
canvas = FigureCanvasTkAgg(figure, win)
canvas.get_tk_widget().grid(row=0, column=0)

win.mainloop()

출력

위의 코드를 실행하면 X 및 Y축에 일부 데이터 포인트가 있는 플롯이 창에 나타납니다.

Tkinter에서 Matplotlib를 어떻게 실행할 수 있습니까?