网站首页 > 开源技术 正文
正弦波公式:
?(x) = a * sin(x + x0) + b
公式中:
a: 决定正弦函数振动幅度的大小;
x0:表示x开始比0拖后的弧度值;
b:表示函数偏离X轴的距离;
对于难以理解的学生来说,可以用动画把这个公式直观的展现出来。
在kimi中输入提示词:
你是一个Python编程专家,完成如下Python脚本的编写:
用GIF、numpy和matplotlib这三个库生成一个关于正弦波函数的gif动画;
GIF图片保存到F盘;
注意:每一步都要输出相关信息到屏幕上
Kimi生成的源代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from PIL import Image
import os
# 设置GIF保存路径
save_path = 'F:\\sin_wave.gif'
# 检查路径是否存在,如果不存在则创建
if not os.path.exists(os.path.dirname(save_path)):
os.makedirs(os.path.dirname(save_path))
print(f'目录 {os.path.dirname(save_path)} 已创建。')
# 初始化绘图
fig, ax = plt.subplots()
x = np.linspace(0, 2 * np.pi, 1000)
y = np.sin(x)
ln, = plt.plot(x, y, 'r-') # 'r-' 表示红色线条
def update(frame):
y = np.sin(2 * np.pi * frame * 0.5 + x)
ln.set_ydata(y)
return ln,
# 创建动画
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2, 60),
blit=True, interval=50)
# 保存动画为GIF
ani.save(save_path, writer='imagemagick', fps=10)
print(f'GIF 动画已保存到 {save_path}')
# 关闭绘图
plt.close()
- 上一篇: 如何把python绘制的动态图形保存为gif文件或视频
- 下一篇: Python动态绘图的方法
猜你喜欢
- 2024-11-17 Python动态绘图的方法(上)
- 2024-11-17 Python动态绘图的方法
- 2024-11-17 如何把python绘制的动态图形保存为gif文件或视频
- 2024-11-17 Java 图片压缩生成缩略图和水印
- 2024-11-17 医疗影像工具LEADTOOLS 入门教程: 使用文档编写器创建文档 - C#
- 2024-11-17 Celluloid让matplotlib动画-2:红绿灯
- 2024-11-17 使用Adobe dng SDK一步一步显示图像
- 2024-11-17 方便!Python 操作 Excel 神器 xlsxwriter 初识
- 2024-11-17 image 用 Rust 编写的图像库——001号RUST库
- 2024-11-17 Qt开源作品11-屏幕录制控件
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)