QQ截图20241114130108.jpg (49.29 KB, 下载次数: 0)
下载附件
2024-11-14 13:03 上传
[Python] 纯文本查看 复制代码import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.animation import FuncAnimation
import tkinter as tk
# 定义函数
def r(theta):
return (np.exp(np.sin(theta))
- 2 * np.cos(4 * theta)
+ np.sin((2 * theta - np.pi) / 24) ** 5)
# 创建 Tkinter 窗口
root = tk.Tk()
root.title("动态绘制极坐标图")
# 创建 Matplotlib 图形
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'}, figsize=(6, 6))
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack()
# 初始化图形
line, = ax.plot([], [], color='red')
ax.set_facecolor('white')
ax.set_title(r'$r = e^{\sin\theta} - 2\cos4\theta + \sin^5\left(\frac{2\theta - \pi}{24}\right)$', va='bottom')
# 创建 theta 的值
theta = np.linspace(0, 24 * np.pi, 5000)
# 初始化函数
def init():
line.set_data([], [])
return line,
# 更新函数
def update(frame):
current_theta = theta[:frame]
current_r = r(current_theta)
line.set_data(current_theta, current_r)
ax.set_ylim(0, np.max(current_r) + 1) # 动态调整 y 轴范围
return line,
# 创建动画
ani = FuncAnimation(fig, update, frames=np.arange(1, len(theta)), init_func=init, blit=True, interval=1)
# 运行 Tkinter 主循环
root.mainloop()