通过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()