交互式函数可视化

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

通过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
    canvas.get_tk_widget().pack()
    # # creating the Matplotlib toolbar
#    toolbar = NavigationToolbar2Tk(canvas, window)
    #toolbar.update()
    # placing the toolbar on the Tkinter window
    canvas.get_tk_widget().pack()
# the main Tkinter window
window = Tk()
# setting the title 
window.title('F(x|theta) visualization - By Joshua')
window.geometry('700x700+0+0')



s = Scale(window, 
            label='theta', 
            from_=theta0, 
            to=theta0_end, 
            orient=HORIZONTAL, 
            length=200, 
            showvalue=0,
            tickinterval=2, 
            resolution=0.01, 
            command= plot)
s.pack()

# label
l = Label(window, 
        bg='green', 
        fg='white', 
        text = 'theta',
        font=('Arial', 12), 
        width=30, 
        height=2)
l.pack()

canvas = FigureCanvasTkAgg(fig, master = window)  
# run the gui
window.mainloop()
import math
from scipy.stats import beta
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(0,1,0.01)
# first parameter start value
a0 = 0.1
# first parameter end value
a0_end = 3
# second parameter start value
b0 = 0.1
# second parameter end value
b0_end = 3
# your function
y = beta.pdf(x, a0, b0)

def plot1(a):
    l1.config(text='alpha: ' + a)
    fig.clear()
    global canvas
    global a0
    a0 = float(a)
    canvas.get_tk_widget().pack_forget()
    #canvas.get_tk_widget().grid_forget()
    plot1 = fig.add_subplot(111)
    # @ this is a key line you need to change, accroading to your function
    y = beta.pdf(x, float(a), b0)
    plot1.plot(x,y)
    canvas = FigureCanvasTkAgg(fig,master = window)  
    canvas.draw()
    canvas.get_tk_widget().pack()
    #canvas.get_tk_widget().grid(row = 3,column=2)

def plot2(b):
    l2.config(text='beta: ' + b)
    fig.clear()
    global canvas
    global b0
    b0 = float(b)
    canvas.get_tk_widget().pack_forget()
    #canvas.get_tk_widget().grid_forget()
    plot1 = fig.add_subplot(111)
    # @ this is a key line you need to change, accroading to your function
    y = beta.pdf(x, a0, float(b))
    plot1.plot(x,y)
    canvas = FigureCanvasTkAgg(fig,master = window)  
    canvas.draw()
    canvas.get_tk_widget().pack()
    #canvas.get_tk_widget().grid(row = 3,column=2)
    
window = Tk()

# setting the title 
window.title('f(x|alpha,beta) visualization - By Joshua')

# dimensions of the main window
window.geometry('700x700+0+0')

# # button that displays the plot
# plot_button = Button(master = window, 
#                      command = plot,
#                      height = 2, 
#                      width = 10,
#                      text = "Plot")
# plot_button.pack()
#
s1 = Scale(window, 
            label='alpha', 
            from_=0.1, 
            to=3, 
            orient=HORIZONTAL, 
            length=200, 
            showvalue=0,
            tickinterval=2, 
            resolution=0.01, 
            command= plot1)
s1.pack()
#s1.grid(row = 1, column = 2)

# 
s2 = Scale(window, 
            label='beta', 
            from_=0.1, 
            to=3, 
            orient=HORIZONTAL, 
            length=200, 
            showvalue=0,
            tickinterval=2, 
            resolution=0.01, 
            command= plot2)
s2.pack()
#s2.grid(row = 2, column = 2)

# 
l1 = Label(window, 
        bg='green', 
        fg='white', 
        text = 'alpha',
        font=('Arial', 12), 
        width=30, 
        height=2)
l1.pack()
#l1.grid(row = 1, column = 1)

l2 = Label(window, 
        bg='blue', 
        fg='white', 
        text = 'beta',
        font=('Arial', 12), 
        width=30, 
        height=2)
l2.pack()
#l2.grid(row = 2, column = 1)

canvas = FigureCanvasTkAgg(fig, master = window)  
# run the gui
window.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ace Cheney

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

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

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

打赏作者

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

抵扣说明:

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

余额充值