import tkinter as tk
from tkinter import ttk, messagebox, simpledialog
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.patches as patches
from matplotlib import ticker
from matplotlib.colors import ListedColormap
import re
import os
plt.rcParams["font.family"] = ["SimHei", "WenQuanYi Micro Hei", "Heiti TC", "Arial Unicode MS"]
plt.rcParams["axes.unicode_minus"] = False
class FunctionVisualizer:
def __init__(self, root):
self.root = root
self.root.title("函数与方程可视化工具")
self.root.geometry("1200x800")
self.root.minsize(1000, 700)
self.bg_color = "#f5f5f5"
self.frame_color = "#ffffff"
self.button_color = "#3b82f6"
self.button_text_color = "#ffffff"
self.explicit_presets = {
"三次函数": {
"func": lambda x: x**3 - 3*x,
"expr": "x**3 - 3*x",
"x_range": (-2.5, 2.5),
"title": "三次函数:$y = x^3 - 3x$",
},
"双曲线": {
"func": lambda x: 1/x,
"expr": "1/x",
"x_range": (-5, 5),
"title": "双曲线:$y = \\frac{1}{x}$",
},
"指数函数": {
"func": lambda x: np.exp(x),
"expr": "np.exp(x)",
"x_range": (-3, 3),
"title": "指数函数:$y = e^x$",
},
}
self.implicit_presets = {
"圆": {
"eq": lambda x, y: x**2 + y**2 - 4,
"expr": "x**2 + y**2 - 4",
"x_range": (-3, 3),
"y_range": (-3, 3),
"title": "圆:$x^2 + y^2 = 4$",
},
"椭圆": {
"eq": lambda x, y: x**2/4 + y**2/9 - 1,
"expr": "x**2/4 + y**2/9 - 1",
"x_range": (-3, 3),
"y_range": (-4, 4),
"title": "椭圆:$\\frac{x^2}{4} + \\frac{y^2}{9} = 1$",
},
"双曲线 (隐式)": {
"eq": lambda x, y: x**2 - y**2 - 1,
"expr": "x**2 - y**2 - 1",
"x_range": (-3, 3),
"y_range": (-3, 3),
"title": "双曲线:$x^2 - y^2 = 1$",
},
"笛卡尔叶形线": {
"eq": lambda x, y: x**3 + y**3 - 3*x*y,
"expr": "x**3 + y**3 - 3*x*y",
"x_range": (-3, 3),
"y_range": (-3, 3),
"title": "笛卡尔叶形线:$x^3 + y^3 = 3xy$",
},
}
main_frame = ttk.Frame(self.root, padding=10)
main_frame.pack(fill=tk.BOTH, expand=True)
left_frame = ttk.LabelFrame(main_frame, text="函数与方程可视化选项", padding=10, width=375)
left_frame.pack(side=tk.LEFT, fill=tk.Y, padx=(0, 10))
left_frame.pack_propagate(False)
right_frame = ttk.Frame(main_frame)
right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
self.plot_frame = ttk.Frame(right_frame)
self.plot_frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
self.fig = Figure(figsize=(8, 6), dpi=100)
self.canvas = FigureCanvasTkAgg(self.fig, master=self.plot_frame)
self.canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)
self.toolbar_frame = ttk.Frame(right_frame, height=40)
self.toolbar_frame.pack(fill=tk.X, padx=5, pady=(0, 5))
self.toolbar = NavigationToolbar2Tk(self.canvas, self.toolbar_frame)
self.toolbar.update()
self.create_controls(left_frame)
self.plot_predefined_function()
def create_controls(self, parent):
"""创建控制选项"""
ttk.Label(parent, text="选择可视化类型:", font=("SimHei", 10, "bold")).pack(anchor=tk.W, pady=(0, 10))
self.viz_type = tk.StringVar(value="explicit")
types = [("显函数", "explicit"), ("隐函数", "implicit"), ("心形线", "heart"), ("电势分布", "potential")]
for text, value in types:
ttk.Radiobutton(parent, text=text, variable=self.viz_type, value=value, command=self.update_controls).pack(anchor=tk.W, padx=5, pady=2)
self.preset_frame = ttk.LabelFrame(parent, text="预设函数", padding=10)
self.preset_frame.pack(fill=tk.X, pady=10)
self.preset_functions = tk.StringVar()
self.preset_combobox = ttk.Combobox(self.preset_frame, textvariable=self.preset_functions, width=30)
self.preset_combobox.pack(fill=tk.X, pady=5)
ttk.Button(self.preset_frame, text="绘制预设函数", command=self.plot_predefined_function).pack(fill=tk.X, pady=5)
self.explicit_frame = ttk.LabelFrame(parent, text="显函数输入", padding=10)
self.explicit_frame.pack(fill=tk.X, pady=10)
ttk.Label(self.explicit_frame, text="函数表达式 (例如 x**2):").pack(anchor=tk.W)
self.explicit_entry = ttk.Entry(self.explicit_frame, width=30)
self.explicit_entry.insert(0, "x**3 - 3*x")
self.explicit_entry.pack(fill=tk.X, pady=5)
ttk.Label(self.explicit_frame, text="X 范围 (min,max):").pack(anchor=tk.W)
self.x_range_entry = ttk.Entry(self.explicit_frame, width=30)
self.x_range_entry.insert(0, "-2.5,2.5")
self.x_range_entry.pack(fill=tk.X, pady=5)
ttk.Button(self.explicit_frame, text="绘制显函数", command=self.plot_explicit).pack(fill=tk.X, pady=5)
self.implicit_frame = ttk.LabelFrame(parent, text="隐函数输入", padding=10)
self.implicit_frame.pack(fill=tk.X, pady=10)
ttk.Label(self.implicit_frame, text="方程表达式 (例如 x**2 + y**2 - 4):").pack(anchor=tk.W)
self.implicit_entry = ttk.Entry(self.implicit_frame, width=30)
self.implicit_entry.insert(0, "x**3 + y**3 - 3*x*y")
self.implicit_entry.pack(fill=tk.X, pady=5)
ttk.Label(self.implicit_frame, text="X 范围 (min,max):").pack(anchor=tk.W)
self.implicit_x_range_entry = ttk.Entry(self.implicit_frame, width=30)
self.implicit_x_range_entry.insert(0, "-3,3")
self.implicit_x_range_entry.pack(fill=tk.X, pady=5)
ttk.Label(self.implicit_frame, text="Y 范围 (min,max):").pack(anchor=tk.W)
self.implicit_y_range_entry = ttk.Entry(self.implicit_frame, width=30)
self.implicit_y_range_entry.insert(0, "-3,3")
self.implicit_y_range_entry.pack(fill=tk.X, pady=5)
ttk.Button(self.implicit_frame, text="绘制隐函数", command=self.plot_implicit).pack(fill=tk.X, pady=5)
ttk.Button(parent, text="保存图像", command=self.save_image).pack(side=tk.BOTTOM, pady=10)
self.update_controls()
def update_controls(self):
"""更新控件状态"""
viz_type = self.viz_type.get()
self.preset_frame.pack_forget()
self.explicit_frame.pack_forget()
self.implicit_frame.pack_forget()
if viz_type == "explicit":
self.explicit_frame.pack(fill=tk.X, pady=10)
self.update_preset_options(self.explicit_presets.keys())
elif viz_type == "implicit":
self.implicit_frame.pack(fill=tk.X, pady=10)
self.update_preset_options(self.implicit_presets.keys())
elif viz_type == "heart":
self.plot_heart_curve()
elif viz_type == "potential":
self.plot_electric_potential()
self.preset_frame.pack(fill=tk.X, pady=10)
def update_preset_options(self, options=None):
"""动态更新预设函数选项"""
if options is None:
options = []
self.preset_combobox["values"] = list(options)
if options:
self.preset_functions.set(list(options)[0])
def plot_predefined_function(self):
"""绘制预设函数"""
viz_type = self.viz_type.get()
selected = self.preset_functions.get()
self.fig.clear()
ax = self.fig.add_subplot(111)
ax.set_facecolor("white")
self.fig.set_facecolor("white")
if viz_type == "explicit" and selected in self.explicit_presets:
data = self.explicit_presets[selected]
self.plot_explicit_function(f=data["func"], x_range=data["x_range"], title=data["title"])
self.explicit_entry.delete(0, tk.END)
self.explicit_entry.insert(0, data["expr"])
self.x_range_entry.delete(0, tk.END)
self.x_range_entry.insert(0, f"{data['x_range'][0]},{data['x_range'][1]}")
elif viz_type == "implicit" and selected in self.implicit_presets:
data = self.implicit_presets[selected]
self.plot_implicit_equation(eq=data["eq"], x_range=data["x_range"], y_range=data["y_range"], title=data["title"])
self.implicit_entry.delete(0, tk.END)
self.implicit_entry.insert(0, data["expr"])
self.implicit_x_range_entry.delete(0, tk.END)
self.implicit_x_range_entry.insert(0, f"{data['x_range'][0]},{data['x_range'][1]}")
self.implicit_y_range_entry.delete(0, tk.END)
self.implicit_y_range_entry.insert(0, f"{data['y_range'][0]},{data['y_range'][1]}")
self.canvas.draw()
def is_valid_expression(self, expr):
"""验证表达式是否为有效的数学表达式"""
allowed_chars = set("0123456789.+-*/()xy^np_sin_cos_tan_exp_sqrt_log_pi_ ")
cleaned = expr.replace('.', '').replace('_', '')
invalid_chars = set(cleaned) - allowed_chars
if invalid_chars:
raise ValueError(f"非法字符:{''.join(invalid_chars)}")
stack = []
for char in expr:
if char == '(':
stack.append(char)
elif char == ')':
if not stack:
raise ValueError("括号不匹配:缺少左括号")
stack.pop()
if stack:
raise ValueError("括号不匹配:缺少右括号")
return True
def safe_eval(self, expr, namespace):
"""安全地执行表达式计算"""
try:
self.is_valid_expression(expr)
expr = expr.replace('^', '**')
allowed_funcs = {
'np': np, 'sin': np.sin, 'cos': np.cos, 'tan': np.tan,
'exp': np.exp, 'sqrt': np.sqrt, 'log': np.log, 'pi': np.pi, 'arctan2': np.arctan2
}
safe_globals = {"__builtins__": None}
safe_locals = {**allowed_funcs, **namespace}
compiled_code = compile(expr, '<string>', 'eval')
return eval(compiled_code, safe_globals, safe_locals)
except Exception as e:
raise ValueError(f"表达式错误:{str(e)}")
def plot_explicit(self):
"""绘制用户输入的显函数"""
try:
func_str = self.explicit_entry.get().strip()
x_range_str = self.x_range_entry.get().strip()
if not func_str or not x_range_str:
raise ValueError("请输入函数表达式和 X 范围")
x_min, x_max = map(float, x_range_str.split(","))
if x_min >= x_max:
raise ValueError("X 范围的最小值必须小于最大值")
x_vals = np.linspace(x_min, x_max, 1000)
y_vals = np.zeros_like(x_vals)
for i, x in enumerate(x_vals):
y_vals[i] = self.safe_eval(func_str, {'x': x})
self.plot_explicit_function(
f=lambda x: y_vals, x_range=(x_min, x_max),
title=f"显函数:$y = {self.get_function_label(func_str)}$",
)
self.canvas.draw()
except Exception as e:
messagebox.showerror("错误", f"绘制显函数时出错:{str(e)}")
def plot_implicit(self):
"""绘制用户输入的隐函数(修复网格点数不匹配问题)"""
try:
eq_str = self.implicit_entry.get().strip()
x_range_str = self.implicit_x_range_entry.get().strip()
y_range_str = self.implicit_y_range_entry.get().strip()
if not eq_str or not x_range_str or not y_range_str:
raise ValueError("请输入完整的方程表达式和范围")
x_min, x_max = map(float, x_range_str.split(","))
y_min, y_max = map(float, y_range_str.split(","))
if x_min >= x_max or y_min >= y_max:
raise ValueError("范围的最小值必须小于最大值")
eq = lambda X, Y: self.safe_eval(eq_str, {'x': X, 'y': Y})
self.plot_implicit_equation(
eq=eq, x_range=(x_min, x_max), y_range=(y_min, y_max),
title=f"隐函数:${self.get_function_label(eq_str)} = 0$",
)
self.canvas.draw()
except Exception as e:
messagebox.showerror("错误", f"绘制隐函数时出错:{str(e)}")
def plot_explicit_function(self, f, x_range=(-5, 5), title="显函数图像"):
"""绘制显函数 y = f(x) 的图像"""
self.fig.clear()
ax = self.fig.add_subplot(111)
ax.set_facecolor("white")
self.fig.set_facecolor("white")
ax.grid(True, linestyle="--", alpha=0.6)
ax.spines["left"].set_position("zero")
ax.spines["bottom"].set_position("zero")
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
x = np.linspace(x_range[0], x_range[1], 1000)
try:
y = f(x)
except Exception as e:
messagebox.showerror("函数错误", f"计算函数值时出错:{str(e)}")
return
ax.plot(x, y, "b-", linewidth=2.5)
ax.set_title(title, fontsize=16, pad=20)
ax.set_xlabel("x", fontsize=12, labelpad=-10, x=1.02)
ax.set_ylabel("y", fontsize=12, labelpad=-20, y=1.02, rotation=0)
self.optimize_ticks(ax, x_range, (np.min(y), np.max(y)))
self.fig.tight_layout()
def plot_implicit_equation(self, eq, x_range=(-3, 3), y_range=(-3, 3), resolution=500, levels=[0], cmap="viridis", title="隐函数图像"):
"""绘制隐函数 F(x, y) = 0 的图像"""
self.fig.clear()
ax = self.fig.add_subplot(111)
ax.set_facecolor("white")
self.fig.set_facecolor("white")
x = np.linspace(x_range[0], x_range[1], resolution)
y = np.linspace(y_range[0], y_range[1], resolution)
X, Y = np.meshgrid(x, y)
try:
Z = eq(X, Y)
except Exception as e:
messagebox.showerror("方程错误", f"计算方程值时出错:{str(e)}")
return
contour = ax.contour(X, Y, Z, levels=levels, colors="red", linewidths=2.5)
if len(levels) > 1:
ax.contourf(X, Y, Z, levels=np.linspace(Z.min(), Z.max(), 100), cmap=cmap, alpha=0.6)
cbar = self.fig.colorbar(contour)
cbar.set_label("F(x, y)", rotation=270, labelpad=20)
ax.grid(True, linestyle="--", alpha=0.4)
ax.set_aspect("equal")
ax.set_title(title, fontsize=16, pad=20)
ax.set_xlabel("x", fontsize=12)
ax.set_ylabel("y", fontsize=12)
ax.axhline(0, color="black", linewidth=0.8, alpha=0.7)
ax.axvline(0, color="black", linewidth=0.8, alpha=0.7)
self.optimize_ticks(ax, x_range, y_range)
self.fig.tight_layout()
def optimize_ticks(self, ax, x_range, y_range):
"""优化坐标轴刻度,避免刻度过于密集"""
x_min, x_max = x_range
y_min, y_max = y_range
x_span = x_max - x_min
y_span = y_max - y_min
x_major_locator = ticker.MaxNLocator(nbins=7)
y_major_locator = ticker.MaxNLocator(nbins=7)
ax.xaxis.set_major_locator(x_major_locator)
ax.yaxis.set_major_locator(y_major_locator)
def plot_heart_curve(self):
"""绘制心形线"""
self.fig.clear()
ax1 = self.fig.add_subplot(111)
ax1.set_aspect("equal")
ax1.set_title("心形线:$(x^2+y^2-1)^3 - x^2y^3 = 0$", fontsize=14)
ax1.set_facecolor("white")
self.fig.set_facecolor("white")
def heart_eq(x, y):
return (x**2 + y**2 - 1)**3 - x**2 * y**3
x = np.linspace(-1.5, 1.5, 500)
y = np.linspace(-1.5, 1.5, 500)
X, Y = np.meshgrid(x, y)
Z = heart_eq(X, Y)
contour = ax1.contour(X, Y, Z, levels=[0], colors="red", linewidths=3)
ax1.contourf(X, Y, Z, levels=[-1000, 0], colors=["pink"], alpha=0.4)
ax1.grid(True, linestyle="--", alpha=0.3)
ax1.set_xlim(-1.5, 1.5)
ax1.set_ylim(-1.5, 1.5)
self.optimize_ticks(ax1, (-1.5, 1.5), (-1.5, 1.5))
self.fig.tight_layout()
self.canvas.draw()
def plot_electric_potential(self):
"""可视化点电荷系统的电势分布"""
self.fig.clear()
ax = self.fig.add_subplot(111)
ax.set_facecolor("white")
self.fig.set_facecolor("white")
charges = [
{"x": -1, "y": 0, "q": 1},
{"x": 1, "y": 0, "q": -1},
]
x = np.linspace(-2.5, 2.5, 500)
y = np.linspace(-2, 2, 500)
X, Y = np.meshgrid(x, y)
V = np.zeros_like(X)
for charge in charges:
r = np.sqrt((X - charge["x"])**2 + (Y - charge["y"])**2)
V += charge["q"] / r
V = np.nan_to_num(V, posinf=10, neginf=-10)
levels = np.linspace(-10, 10, 21)
contourf = ax.contourf(X, Y, V, levels=levels, cmap="coolwarm", alpha=0.8)
contour = ax.contour(X, Y, V, levels=levels, colors="k", linewidths=0.5)
ax.clabel(contour, inline=True, fontsize=8)
for charge in charges:
color = "red" if charge["q"] > 0 else "blue"
marker = "+" if charge["q"] > 0 else "_"
ax.scatter(charge["x"], charge["y"], s=300, c=color, marker=marker, linewidths=2)
ax.text(charge["x"], charge["y"]+0.2, f"{charge['q']}q", ha="center", fontsize=12, weight="bold")
ax.set_title("两个点电荷的电势分布", fontsize=16, pad=20)
ax.set_xlabel("x (m)", fontsize=12)
ax.set_ylabel("y (m)", fontsize=12)
ax.set_aspect("equal")
ax.grid(True, linestyle="--", alpha=0.4)
ax.axhline(0, color="k", linewidth=0.8, alpha=0.7)
ax.axvline(0, color="k", linewidth=0.8, alpha=0.7)
ax.text(1.5, 1.8, r"$V = \sum \frac{kq_i}{r_i}$", fontsize=14, bbox=dict(facecolor="white", alpha=0.8))
cbar = self.fig.colorbar(contourf, label="电势 (V)")
self.optimize_ticks(ax, (-2.5, 2.5), (-2, 2))
self.fig.tight_layout()
self.canvas.draw()
def get_function_label(self, func_str):
"""生成函数的 LaTeX 标签"""
if any(word in func_str.lower() for word in ["import", "os", "sys", "subprocess"]):
raise ValueError("检测到不安全的代码")
safe_str = func_str
replacements = {
r'np\.sin\(([^)]+)\)': r'\sin(\1)',
r'np\.cos\(([^)]+)\)': r'\cos(\1)',
r'np\.tan\(([^)]+)\)': r'\tan(\1)',
r'np\.exp\(([^)]+)\)': r'\exp(\1)',
r'np\.sqrt\(([^)]+)\)': r'\sqrt{\1}',
r'np\.log\(([^)]+)\)': r'\ln(\1)',
r'np\.pi': r'\pi',
r'\*\*': r'^',
r'\*': r'\cdot',
}
for pattern, replacement in replacements.items():
try:
safe_str = re.sub(pattern, replacement, safe_str)
except re.error as e:
continue
if '/' in safe_str:
if re.search(r'\d+\.?\d*/\d+\.?\d*', safe_str):
parts = safe_str.split('/')
if len(parts) == 2:
numerator = parts[0].strip()
denominator = parts[1].strip()
safe_str = r'\frac{' + numerator + '}' + r'{'+ denominator +'}'
return safe_str
def save_image(self):
"""保存当前图像"""
try:
filename = simpledialog.askstring("保存图像", "请输入文件名:", initialvalue="function_plot.png")
if filename:
if not filename.endswith(".png"):
filename += ".png"
self.fig.savefig(filename, dpi=150, bbox_inches="tight")
messagebox.showinfo("成功", f"图像已保存至:{os.path.abspath(filename)}")
except Exception as e:
messagebox.showerror("保存错误", f"保存图像时出错:{e}")
def main():
root = tk.Tk()
style = ttk.Style()
style.configure("TFrame", background="#f5f5f5")
style.configure("TLabelframe", background="#ffffff", relief="sunken")
style.configure("TLabelframe.Label", background="#ffffff", font=("SimHei", 10, "bold"))
style.configure("TButton", padding=5)
try:
plt.rcParams["font.family"] = ["SimHei"]
except:
try:
plt.rcParams["font.family"] = ["WenQuanYi Micro Hei"]
except:
try:
plt.rcParams["font.family"] = ["Heiti TC"]
except:
try:
plt.rcParams["font.family"] = ["Arial Unicode MS"]
except:
plt.rcParams["font.family"] = ["DejaVu Sans", "sans-serif"]
print("警告:未找到中文字体,图表文字可能无法正确显示")
app = FunctionVisualizer(root)
root.mainloop()
if __name__ == "__main__":
main()