在tkinter画布中插入交互式matplotlib图形,可以通过以下步骤实现:
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4, 5], [2, 4, 6, 8, 10])
canvas = FigureCanvasTkAgg(fig, master=canvas)
canvas.draw()
canvas.get_tk_widget().pack()
toolbar = NavigationToolbar2Tk(canvas, canvas)
toolbar.update()
canvas.get_tk_widget().pack()
完整的代码示例:
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4, 5], [2, 4, 6, 8, 10])
canvas = FigureCanvasTkAgg(fig, master=canvas)
canvas.draw()
canvas.get_tk_widget().pack()
toolbar = NavigationToolbar2Tk(canvas, canvas)
toolbar.update()
canvas.get_tk_widget().pack()
tk.mainloop()
这样就可以在tkinter画布中插入交互式的matplotlib图形了。
推荐的腾讯云相关产品:腾讯云服务器(CVM)和云数据库MySQL。
领取专属 10元无门槛券
手把手带您无忧上云