2016-01-30 12 views
11

下のコードからアニメーションヒストグラムを作成しようとしています。私は毎回個別のヒストグラムを作成することができますが、matplotlib.animation関数を使ってアニメートするか、matplotlib tutorialのコードをエミュレートすることはできません。Matplotlibアニメーションヒストグラム

import numpy as np 
import matplotlib.pyplot as plt 


betas = [] # some very long list 
entropy = [] # some very long list 

for time in [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0 , 3.5, 4.0, 4.5 5.0, 5.5, 6.0, 6.5 , 7.0, 7.5, 8.0 , 8,5 , 9.0, 9.5 , 10.0]: 

    plt.figure('entropy distribution at time %s ' % time)   
    indexbetas = {i for i, j in enumerate(betas) if j == time} 
    desiredentropies = [x for i, x in enumerate(entropy) if i in indexbetas] #the desiredentropies list depends on time 

    n, bins, patches = plt.hist(desiredentropies, 20, alpha=0.75 , label = 'desired entropies') 

plt.xlabel(r"$S_{(\time=%d)}$" % time, fontsize=20) 
plt.ylabel('Frequency of entropies') 


plt.legend() 
plt.grid(True) 
plt.show() 

私はアニメーションのtimeリストの要素に依存して私のdesiredentropiesリストを供給するとともに、特に苦労しています。

+0

チュートリアルの例は、Python 2.7.11と3.4.3/Linux Mintで動作するには数秒必要です。console/terminal/cmd.exeにエラーメッセージが表示されますか? – furas

+0

@furas OPに編集を追加しました。私の主な問題は、時間の変化に応じて、ヒストグラムを作成したい、更新されたdesiredentropiesリストを与えることです。注:必要なエントロピーリストは、各要素の時間変化を示します。 –

+0

'animation.FuncAnimation'を使用する必要があります – furas

答えて

5

これを試してください。これは基本的にFuncAnimationを利用してヒストグラムを更新できるようにします。 animation documentationをチェックして、その機能のさまざまなパラメータの詳細を知り、アップデートの速度やそのようなものを制御してください。我々はここで何

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

n = 100 
number_of_frames = 10 
data = np.random.rand(n, number_of_frames) 

def update_hist(num, data): 
    plt.cla() 
    plt.hist(data[num]) 

fig = plt.figure() 
hist = plt.hist(data[0]) 

animation = animation.FuncAnimation(fig, update_hist, number_of_frames, fargs=(data,)) 
plt.show() 

は、ヒストグラムを更新し、各ステップで新しいデータを表示処理する関数、update_histを呼び出すです。これを行うには、軸を消去して、提供されたnum(現在のフレーム番号)を使用してデータにインデックスを作成します。

+0

リストは時刻、つまり時間リストの要素に依存しているので、新しい優先順位リストを更新するにはどうすればよいですか? –

+0

私はこれを私のコードに適合させるのに苦労しています。 –

+0

あなたのインデックスを整数に正規化し、あなたの 'for'ループに' update_hist'にコードを入れてください。 –