You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
47 lines
1.3 KiB
1 year ago
|
import tkinter as tk
|
||
|
|
||
|
import sympy
|
||
|
from matplotlib.figure import Figure
|
||
|
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||
|
from mpl_toolkits.mplot3d import Axes3D
|
||
|
import numpy as np
|
||
|
from sympy import symbols, lambdify
|
||
|
|
||
|
|
||
|
def plot_3d_graph(expression):
|
||
|
# 创建 sympy 符号
|
||
|
x, y = symbols('x y')
|
||
|
# 将表达式转换为可计算的函数
|
||
|
expr = lambdify((x, y), expression, modules=['numpy'])
|
||
|
# 创建数据网格
|
||
|
X = np.linspace(-10, 10, 1000)
|
||
|
Y = np.linspace(-10, 10, 1000)
|
||
|
X, Y = np.meshgrid(X, Y)
|
||
|
Z = expr(X, Y)
|
||
|
# 创建一个 Figure 对象
|
||
|
fig = Figure(figsize=(6, 6), dpi=100)
|
||
|
# 在 Figure 上创建一个 3D 子图
|
||
|
ax = fig.add_subplot(111, projection='3d')
|
||
|
# 绘制三维图形
|
||
|
ax.plot_surface(X, Y, Z, cmap='viridis')
|
||
|
|
||
|
# 创建一个 tkinter 窗口
|
||
|
root = tk.Tk()
|
||
|
root.title("3D Graph")
|
||
|
|
||
|
# 在 tkinter 窗口上创建一个 canvas
|
||
|
canvas = FigureCanvasTkAgg(fig, master=root)
|
||
|
canvas.draw()
|
||
|
canvas.get_tk_widget().pack()
|
||
|
|
||
|
# 运行 tkinter 主循环
|
||
|
tk.mainloop()
|
||
|
|
||
|
# 输入要绘制的数学公式
|
||
|
expression = "gamma(x)"
|
||
|
|
||
|
# 使用 sympy 解析数学公式
|
||
|
parsed_expression = sympy.sympify(expression)
|
||
|
|
||
|
# 调用函数绘制三维图形
|
||
|
plot_3d_graph(parsed_expression)
|