当前位置: 首页 > 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 动画效果

相关文章:

  • 黑马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章 项目风险管理 (总章)
  • 东部沿海大省浙江,为何盯上内河航运?
  • 澎湃与七猫联合启动百万奖金征文,赋能非虚构与现实题材创作
  • 一周文化讲座|“我的生命不过是温柔的疯狂”
  • 共建医学人工智能高地,上海卫健委与徐汇区将在这些方面合作
  • 多条跨境铁路加速推进,谁是下一个“超级枢纽”?
  • 费高云不再担任安徽省人民政府副省长