交互式函数可视化

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

通过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
 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ace Cheney

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值