如何在CustomTkinter与Matplotlib中实现双函数同图绘制?
如何在CustomTkinter中让Matplotlib同时显示三角和梯形隶属函数
我是Python初学者,正在学习结合CustomTkinter与Matplotlib绘图。现有代码可通过点击不同按钮分别绘制三角隶属函数(Triangle function)和梯形隶属函数(Trapz function),想实现将两个函数显示在同一幅图中,请问该怎么做?
原代码如下:
from tkinter import * from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk) import customtkinter as ctk import numpy as np def plot(MF): fig = Figure() plot1= fig.add_subplot(111) fig.set_facecolor("grey") fig.set_edgecolor("blue") plot1.plot(MF) canvas = FigureCanvasTkAgg(fig, win) canvas.draw() toolbar = NavigationToolbar2Tk(canvas, win) toolbar.update() toolbar.place(relx=0.2, rely=0.4) canvas.get_tk_widget().place(relx=0.05, rely=0.4) def Triangle_function(): a = 100 b = 500 c = 700 MF = np.zeros(1000) for x in range(1000): if x < a: MF[x] = 0 elif x >= a and x <= b: MF[x] = (x-a)/(b-a) elif x >= b and x <= c: MF[x] = (c-x)/(c-b) elif x > c: MF[x] = 0 plot (MF) def Trapz(): a = 100 b = 500 c = 700 d = 900 MF = np.zeros(1000) for x in range(1000): if x < a: MF[x] = 0 elif x >= a and x < b: MF[x] = (x-a)/(b-a) elif x >= b and x < c: MF[x] = 1 elif x >= c and x < d: MF[x] = (d-x)/(d-c) elif x >= d: MF[x] = 0 plot(MF) win = ctk.CTk() win.geometry("1000x1000") win.title("Fuzzy Logic Designer") win.resizable(False, False) plot_Button = ctk.CTkButton(win, text="plot", command = Triangle_function) plot_Button.pack() plot_Button1 = ctk.CTkButton(win, text="plot", command = Trapz) plot_Button1.pack(pady=20) win.mainloop()
解决方案
你可以通过以下修改实现两个函数同图显示:
修改后的完整代码
from tkinter import * from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk) import customtkinter as ctk import numpy as np # 重构plot函数,支持传入多个隶属函数数据 def plot(*MFs): fig = Figure() plot1 = fig.add_subplot(111) fig.set_facecolor("grey") fig.set_edgecolor("blue") # 遍历所有传入的隶属函数,分别绘制并添加标签 labels = ["三角隶属函数", "梯形隶属函数"] for idx, mf in enumerate(MFs): plot1.plot(mf, label=labels[idx] if idx < len(labels) else f"函数{idx+1}") # 添加图例方便区分 plot1.legend() canvas = FigureCanvasTkAgg(fig, win) canvas.draw() toolbar = NavigationToolbar2Tk(canvas, win) toolbar.update() toolbar.place(relx=0.2, rely=0.4) canvas.get_tk_widget().place(relx=0.05, rely=0.4) def Triangle_function(): a = 100 b = 500 c = 700 MF = np.zeros(1000) for x in range(1000): if x < a: MF[x] = 0 elif x >= a and x <= b: MF[x] = (x-a)/(b-a) elif x >= b and x <= c: MF[x] = (c-x)/(c-b) elif x > c: MF[x] = 0 plot(MF) def Trapz(): a = 100 b = 500 c = 700 d = 900 MF = np.zeros(1000) for x in range(1000): if x < a: MF[x] = 0 elif x >= a and x < b: MF[x] = (x-a)/(b-a) elif x >= b and x < c: MF[x] = 1 elif x >= c and x < d: MF[x] = (d-x)/(d-c) elif x >= d: MF[x] = 0 plot(MF) # 新增函数:同时生成两个隶属函数并传入plot def plot_both(): # 生成三角隶属函数数据 a_t, b_t, c_t = 100, 500, 700 mf_triangle = np.zeros(1000) for x in range(1000): if x < a_t: mf_triangle[x] = 0 elif x >= a_t and x <= b_t: mf_triangle[x] = (x-a_t)/(b_t-a_t) elif x >= b_t and x <= c_t: mf_triangle[x] = (c_t-x)/(c_t-b_t) elif x > c_t: mf_triangle[x] = 0 # 生成梯形隶属函数数据 a_tr, b_tr, c_tr, d_tr = 100, 500, 700, 900 mf_trapz = np.zeros(1000) for x in range(1000): if x < a_tr: mf_trapz[x] = 0 elif x >= a_tr and x < b_tr: mf_trapz[x] = (x-a_tr)/(b_tr-a_tr) elif x >= b_tr and x < c_tr: mf_trapz[x] = 1 elif x >= c_tr and x < d_tr: mf_trapz[x] = (d_tr-x)/(d_tr-c_tr) elif x >= d_tr: mf_trapz[x] = 0 # 同时传入两个数据 plot(mf_triangle, mf_trapz) win = ctk.CTk() win.geometry("1000x1000") win.title("Fuzzy Logic Designer") win.resizable(False, False) # 修改按钮文本,功能更明确 plot_tri_btn = ctk.CTkButton(win, text="绘制三角隶属函数", command=Triangle_function) plot_tri_btn.pack() plot_trapz_btn = ctk.CTkButton(win, text="绘制梯形隶属函数", command=Trapz) plot_trapz_btn.pack(pady=20) # 新增同时绘制的按钮 plot_both_btn = ctk.CTkButton(win, text="同时绘制两个隶属函数", command=plot_both) plot_both_btn.pack(pady=10) win.mainloop()
关键改动说明
- 重构
plot函数:将参数改为*MFs(可变长度参数),支持一次性传入多个隶属函数数组。循环遍历每个数组进行绘制,并添加标签和图例,方便区分不同函数。 - 新增
plot_both函数:分别生成三角和梯形隶属函数的数据,然后一起传入plot函数实现同图显示。 - 优化按钮文本:将原有按钮的“plot”改为明确的功能描述,避免混淆;新增“同时绘制两个隶属函数”按钮,触发合并绘制逻辑。
内容的提问来源于stack exchange,提问作者Luka Tekk
相关产品推荐
相关产品推荐

