はじめに
速さと距離の関係をアニメーションにして示したかったので、manimで作ってみました。
コードはjupyter notebook(あるいは、google colab(後述))で動かしてください。notebook以外では1行目のマジックコマンド(%%で始まるやつ)が使えません。
gif
(色がおかしく見えるので、youtubeのリンクを貼っておきます。
https://www.youtube.com/watch?v=KiGbJkFSgHw )
コード
自分の環境がある人はコピペするだけで使えます。
%%manim -qm RiemanIntegral
class RiemanIntegral(Scene):
def construct(self):
axes = Axes(
x_range=[-0.5, 5],
x_length=8,
y_range=[-0.5, 10],
y_length=7
).add_coordinates()
axes_labels = axes.get_axis_labels(
MathTex("t").scale(1), MathTex("v").scale(1)
)
graph = axes.plot(
lambda x: 0.4*(x-1) * (x-3) * (x-4) +6,
x_range=[-0.5, 5],
color=YELLOW
)
graph_label = axes.get_graph_label(
graph=graph,
label=MathTex("v(t)")
)
self.add(axes, graph, axes_labels, graph_label)
#dividing setting
dx_list = [4, 1, 0.5, 0.3, 0.1, 0.05, 0.01]
rectangles = VGroup(
*[
axes.get_riemann_rectangles(
graph=graph,
x_range=[0, 4],
dx=dx,
stroke_width=0.1,
stroke_color=WHITE,
)
for dx in dx_list
]
)
first_area = rectangles[0]
for k in range(1, len(dx_list)):
new_area = rectangles[k]
self.play(Transform(first_area, new_area), run_time=2)
self.wait(0.5)
self.wait()
コードの説明については、私も完全に理解しているわけではないので省略します。pythonの文法を知っていればなんとなくわかると思います。
質問があればぜひしてください。私がわかる範囲で頑張って答えます。
google colabで使うために
自分のパソコンに自分の環境を作るのは少し手間がかかるので、google colabでの使い方を説明します。
google colabを使うにはgoogleアカウントが必要なので持っていない方は作成してください。
https://docs.manim.community/en/stable/installation/jupyter.html#google-colaboratory に書いてあるコードをコピペしてgoogle colabで実行するだけです。
以下、引用
!sudo apt update !sudo apt install libcairo2-dev ffmpeg \ texlive texlive-latex-extra texlive-fonts-extra \ texlive-latex-recommended texlive-science \ tipa libpango1.0-dev !pip install manim !pip install IPython==8.21.0
このコードを実行した後、新しいセルで次のコードを実行する:
from manim import *
実行した後はセルに上のセクションのコード(RiemannIntegral)をコピペして実行してください。
参考文献
manimの公式サイトに使い方は載っています。
かなりお世話になったyoutubeチャンネルです。ただし、少しversionが古いので使えないものもあります。公式サイトできちんと確認しておくようにしましょう。