logo

Python でのグラフプロット |セット3

Python でのグラフプロット |セット1 Python でのグラフプロット |セット2 Matplotlib は、以下をサポートする非常に広範なライブラリです。 アニメーション グラフも同様に。アニメーション ツールは以下を中心としています。 matplotlib.アニメーション アニメーション機能が構築されるフレームワークを提供する基本クラス。主なインターフェースは次のとおりです。 時限アニメーション そして 機能アニメーション そしてその2つのうち 機能アニメーション が最も使いやすいです。 インストール:
    マットプロットリブ:参照 Python でのグラフプロット |セット1 ナンピー: You can install numpy module using following pip command:
    pip install numpy
    FFMPEG: アニメーションをビデオとして保存する場合にのみ必要です。実行可能ファイルは次からダウンロードできます。 ここ
実装: Python
# importing required modules import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np # create a figure axis and plot element fig = plt.figure() ax = plt.axes(xlim=(-50 50) ylim=(-50 50)) line = ax.plot([] [] lw=2) # initialization function def init(): # creating an empty plot/frame line.set_data([] []) return line # lists to store x and y axis points xdata ydata = [] [] # animation function def animate(i): # t is a parameter t = 0.1*i # x y values to be plotted x = t*np.sin(t) y = t*np.cos(t) # appending new points to x y axes points list xdata.append(x) ydata.append(y) # set/update the x and y axes data line.set_data(xdata ydata) # return line object return line # setting a title for the plot plt.title('A growing coil!') # hiding the axis details plt.axis('off') # call the animator  anim = animation.FuncAnimation(fig animate init_func=init frames=500 interval=20 blit=True) # save the animation as mp4 video file anim.save('animated_coil.mp4' writer = 'ffmpeg' fps = 30) # show the plot plt.show() 
Here is how the output animation looks like: Now let us try to understand the code in pieces:
  • fig = plt.figure() ax = plt.axes(xlim=(-50 50) ylim=(-50 50)) line = ax.plot([] [] lw=2)
    Here we first create a figure i.e a top level container for all our subplots. Then we create an axes element これはサブプロットとして機能します。 x 軸と y 軸の範囲/制限も、axes 要素の作成時に定義されます。最後に、 プロット という名前の要素 ライン 。最初に、x 軸と y 軸の点は空のリストと線幅として定義されています。 (lw) 2に設定されています。
  • def init(): line.set_data([] []) return line
    Now we declare a initialization function 。この関数は、最初のフレームを作成するためにアニメータによって呼び出されます。
  • def animate(i): # t is a parameter t = 0.1*i # x y values to be plotted x = t*np.sin(t) y = t*np.cos(t) # appending new points to x y axes points list xdata.append(x) ydata.append(y) # set/update the x and y axes data line.set_data(xdata ydata) # return line object return line
    This is the most important function of above program. アニメート() この関数はアニメーターによって何度も呼び出され、各フレームが作成されます。この関数が呼び出される回数は、次のように渡されるフレーム数によって決まります。 フレーム アニメーターへの反論。 アニメーション化() function takes the index of ith frame as argument.
    t = 0.1*i
    Here we cleverly use the index of current frame as a parameter!
    x = t*np.sin(t) y = t*np.cos(t)
    Now since we have the parameter t we can easily plot any parametric equation. For example here we are plotting a spiral using its parametric equation.
    line.set_data(xdata ydata) return line
    Finally we use set_data() x および y データを設定し、プロット オブジェクトを返す関数 ライン
  • anim = animation.FuncAnimation(fig animate init_func=init frames=500 interval=20 blit=True)
    Now we create the FuncAnimation object 。以下で説明するさまざまな引数を受け取ります。 イチジク : プロットする図形。 アニメーション化する : フレームごとに繰り返し呼び出される関数 init_func : クリアフレームを描画する関数です。最初のフレームの前に 1 回呼び出されます。 フレーム : フレーム数。 (注記: フレーム イテラブルまたはジェネレーターにすることもできます。) 間隔 : フレーム間の継続時間 (ミリ秒単位) 滞在する : blit=True に設定すると、変更された部分のみが描画されることになります。
  • anim.save('animated_coil.mp4' writer = 'ffmpeg' fps = 30)
    Now we save the animator object as a video file using 保存() 関数。アニメ動画を保存するにはムービーライターが必要です。この例では、FFMPEG ムービー ライターを使用しました。それで ライター 「ffmpeg」として設定されます。 fps 1 秒あたりのフレーム数を表します。
例 2 This example shows how one can make a rotating curve by applying some simple mathematics! Python
# importing required modules import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np # create a figure axis and plot element fig = plt.figure() ax = plt.axes(xlim=(-25 25) ylim=(-25 25)) line = ax.plot([] [] lw=2) # initialization function def init(): # creating an empty plot/frame line.set_data([] []) return line # set of points for a star (could be any curve) p = np.arange(0 4*np.pi 0.1) x = 12*np.cos(p) + 8*np.cos(1.5*p) y = 12*np.sin(p) - 8*np.sin(1.5*p) # animation function def animate(i): # t is a parameter t = 0.1*i # x y values to be plotted X = x*np.cos(t) - y*np.sin(t) Y = y*np.cos(t) + x*np.sin(t) # set/update the x and y axes data line.set_data(X Y) # return line object return line # setting a title for the plot plt.title('A rotating star!') # hiding the axis details plt.axis('off') # call the animator  anim = animation.FuncAnimation(fig animate init_func=init frames=100 interval=100 blit=True) # save the animation as mp4 video file anim.save('basic_animation.mp4' writer = 'ffmpeg' fps = 10) # show the plot plt.show() 
Here is how the output of above program looks like: Here we have used some simple mathematics to rotate a given curve.
  • 星形は、k = 2.5 と 0 を置くことによって得られます。p = np.arange(0 4*np.pi 0.1) x = 12*np.cos(p) + 8*np.cos(1.5*p) y = 12*np.sin(p) - 8*np.sin(1.5*p)
  • Now in each frame we rotate the star curve using concept of rotation in complex numbers. Let x y be two ordinates. Then after rotation by angle theta the new ordinates are: {x}' = xcos theta - ysin theta {y}' = xsin theta + ycos theta The same has been applied here:
    X = x*np.cos(t) - y*np.sin(t) Y = y*np.cos(t) + x*np.sin(t)
全体として、アニメーションは素晴らしいものを作成するための優れたツールであり、アニメーションを使用してさらに多くのものを作成できます。以上が、Matplotlib を使用してアニメーション プロットを生成および保存する方法でした。 クイズの作成