通过tkinter 内嵌 matplotlib实现
本脚本GitHub链接


import math
import numpy as np
from tkinter import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)
fig = Figure(figsize = (5, 5), dpi = 100)
# the value range of x
x = np.arange(1,10,0.01)
# parameter start value
theta0 = 0
# parameter end value
theta0_end = 10
def plot(v):
l.config(text='theta: ' + v)
fig.clear()
global canvas
canvas.get_tk_widget().pack_forget()
# @ this is a key line you need to change, accroading to your function
y = [math.sin(float(v)*i) for i in x]
# adding the subplot
plot1 = fig.add_subplot(111)
# plotting the graph
plot1.plot(x,y)
# creating the Tkinter canvas
# containing the Matplotlib figure
canvas = FigureCanvasTkAgg(fig, master = window)
canvas.draw()
# placing the canvas on the Tkinter window

这篇博客展示了如何结合Python的Tkinter库和Matplotlib库创建一个GUI应用,实现实时调整参数并动态更新图形的功能。作者通过两个示例分别演示了如何根据滑动条的值改变正弦函数的参数以及贝塔分布的参数,实时显示图形变化。这个应用对于理解和可视化数学函数以及概率分布的变化非常有帮助。
最低0.47元/天 解锁文章
1937

被折叠的 条评论
为什么被折叠?



