当前位置: 首页 > news >正文

【Manim】使用manim画一个高斯分布的动画

1 Manim例子一

最近接触到manim,觉得挺有趣的,来玩一玩把。如下是一个使用manim画的高斯分布的动画。

from manim import *
import numpy as npclass GaussianDistribution(Scene):def construct(self):# 创建坐标系axes = Axes(x_range=[-4, 4, 1],y_range=[0, 0.5, 0.1],axis_config={"color": BLUE},)# 添加坐标轴标签labels = axes.get_axis_labels(x_label=MathTex("x"),y_label=MathTex("P(x)"))# 定义高斯分布函数def gaussian(x, mu=0, sigma=1):return (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x - mu) / sigma) ** 2)# 创建高斯分布曲线gaussian_curve = axes.plot(lambda x: gaussian(x),color=YELLOW,x_range=[-4, 4])# 创建曲线方程标签equation = MathTex("P(x) = \\frac{1}{\\sigma\\sqrt{2\\pi}} e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}",color=YELLOW).scale(0.8).to_edge(UP)# 添加动画self.play(Create(axes), Write(labels))self.play(Write(equation))self.play(Create(gaussian_curve), run_time=2)# 添加说明文字description = Text("高斯分布(正态分布):μ=0, σ=1",font="SimSun").scale(0.5).to_edge(DOWN)self.play(Write(description))# 添加动态效果:显示不同参数的高斯分布sigma_values = [0.5, 1, 2]colors = [RED, YELLOW, GREEN]for sigma, color in zip(sigma_values, colors):new_curve = axes.plot(lambda x: gaussian(x, sigma=sigma),color=color,x_range=[-4, 4])new_description = Text(f"高斯分布:μ=0, σ={sigma}",font="SimSun").scale(0.5).to_edge(DOWN)self.play(Transform(gaussian_curve, new_curve),Transform(description, new_description),run_time=1.5)self.wait(0.5)self.wait(2)if __name__ == "__main__":with tempconfig({"preview": True,"quality": "medium_quality","format": "mp4"}):scene = GaussianDistribution()scene.render()``````bash
2动画效果

2 动画效果

http://www.dtcms.com/a/195325.html

相关文章:

  • 黑马k8s(八)
  • Spring MVC 对 JavaWeb 的优化:从核心组件到注解
  • 使用 LSTM/GRU 预测设备异常的模型
  • 前端 vue 部署 nginx 请求 404
  • MCP概述及MCP Server的使用和实现(谷歌ADK使用MCP Server)
  • P6123 [NEERC 2016] Hard Refactoring 题解
  • Invicti-Professional-V25.5
  • C/C++实践(九)C++二叉搜索树深入讲解
  • 高效批量合并Word文档的工具介绍
  • FC7300 PWM MCAL配置引导
  • 关于计算机系统和数据原子性的联系
  • Redis 五种类型基础操作(redis-cli + Spring Data Redis)
  • 反编译读取.class文件
  • 从微积分到集合论(1630-1910)(历史简介)——第1章——积分技巧(1630-1660)(Kirsti Møller Pedersen)
  • 时源芯微|磁珠
  • PCL 计算一条射线与二次曲面的交点
  • 【Unity】 HTFramework框架(六十五)ScrollList滚动数据列表
  • 显性知识的主要特征
  • HNU工训--计算机串口数据收发与测量
  • 2025年PMP 学习十六 第11章 项目风险管理 (总章)
  • 如何在自动化脚本中向控件输入文本?
  • ohttps开启群晖ssl证书自动更新
  • Leetcode76覆盖最小子串
  • 五月份嵌入式面试总结
  • 锐捷交换机STP环路日志信息解读
  • ODB 的安装及使用
  • 前端实现流式输出《后端返回Markdown格式文本,前端输出类似于打字的那种》
  • Systemd基础
  • qtc++ qdebug日志生成
  • 【gRPC】HTTP/2协议,HTTP/1.x中线头阻塞问题由来,及HTTP/2中的解决方案,RPC、Protobuf、HTTP/2 的关系及核心知识点汇总