Python でのプロットのアニメーション化
この記事では、 Matplotlibを使用して Python でプロットをアニメーション化する方法を説明します。これにより、ビジュアライゼーションに素晴らしいタッチを加えることができます。
マットプロットリブ
Matplotlib は、Python プログラミング言語のプロット パッケージです。これは、Python のビジュアライゼーションで最も人気のあるパッケージの 1 つであり、ユーザーに 2-D、3-D、さらにはアニメーション プロットを作成する機能を提供します。Matplotlib は、折れ線プロット、散布図、棒グラフ、ヒストグラムなどのプロットを作成するためのさまざまなカスタマイズ可能なオプションを提供します。多くの人は静的視覚化に Matplotlib を使用することにすでに慣れているため、アニメーション プロットでの Matplotlib の使用について説明します。Matplotlib を初めて使用するかもしれないが、すでに Python に精通している人にとって、パッケージの使用を開始するための優れたリソースがここにあります。
基本的な 2D アニメーション プロット
#Empty lists for X/Y data
x = []
y = []
#Create a figure and axis to plot on
fig, ax = plt.subplots()
#We will create a function that will be called by Matplotlib's FuncAnimation
def getAnimation(iterator):
newInt = random.randint(1,10)
#Append values to our lists
x.append(iterator)
y.append(newInt)
#Clear previous data from the plot
ax.clear()
#Plot the lists with axis limits
ax.plot(x, y)
ax.set_xlim([0,20])
ax.set_ylim([0,20])
#We pass FuncAnimation our getAnimation function so that it is called in a loop
animation = FuncAnimation(fig, getAnimation, frames=20,
interval=500, repeat=False)
plt.show()
Matplotlib を通じて提供されるFuncAnimation()には、いくつかのフレームを含むループで実行される Figure と関数が必要です。getAnimation()を引数として渡すことにより、基本的に、アニメーションを形成するために結合されるプロットのスライドショーを作成し、フレームの回数だけループします。これが私たちのコードの結果です…
ランダムな 3D 歩行アニメーション
#Empty lists for X/Y/Z data
x = []
y = []
z = []
#Lets start off at (10,10,10) the center of our figure with axis limits 20
x.append(10)
y.append(10)
z.append(10)
#Total number of steps we will take
stepTotal = 20
#Create a figure and axis to plot on (3-D)
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
#We will create a function that will be called by Matplotlib's FuncAnimation
def randomWalk(step):
#Append values to our lists
x.append(random.uniform(1,10))
y.append(random.uniform(1,10))
z.append(random.uniform(1,10))
#Clear previous data from the plot
ax.clear()
#Plot the lists with axis limits
ax.plot(x, y, z)
ax.set_xlim([0,20])
ax.set_ylim([0,20])
ax.set_zlim([0,20])
# Creating the Animation object
animation = FuncAnimation(fig, randomWalk, frames=stepTotal, interval=100)
plt.show()
アニメーションの保存
#FuncAnimation() returns an animation object that can be stored
animation = FuncAnimation()
#Instantiate the directory you wish to save to
directory = "/medium/animation.gif"
#We call .save() on our animation object and pass the function a directory
animation.save(directory)
この記事のすべてのコードは私のGithubにあります。
この Medium の記事が興味深いと思われた場合は、私をフォローすることを検討してください。数学、コンピューターサイエンス、そしてその間のあらゆることについて書いています。

![とにかく、リンクリストとは何ですか?[パート1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































