3

データには2Dのhistogram2dが付属しているため、データをアニメーション化する必要があります(おそらく3D以降ですが、mayaviの方が優れていると聞いています)。

コードは次のとおりです。

import numpy as np
import numpy.random
import matplotlib.pyplot as plt
import time, matplotlib


plt.ion()

# Generate some test data
x = np.random.randn(50)
y = np.random.randn(50)

heatmap, xedges, yedges = np.histogram2d(x, y, bins=5)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

# start counting for FPS
tstart = time.time()

for i in range(10):

    x = np.random.randn(50)
    y = np.random.randn(50)

    heatmap, xedges, yedges = np.histogram2d(x, y, bins=5)

    plt.clf()
    plt.imshow(heatmap, extent=extent)
    plt.draw()

# calculate and print FPS
print 'FPS:' , 20/(time.time()-tstart)

3 fps を返しますが、明らかに遅すぎます。各反復で numpy.random を使用していますか? ブリットを使用する必要がありますか? もしそうなら、どのように?

ドキュメントにはいくつかの良い例がありますが、私にとってはすべてが何をするのかを理解する必要があります。

4

2 に答える 2

6

@Chris のおかげで、もう一度例を見て、この信じられないほど役立つ投稿を見つけました。

@bmuがanimation.FuncAnimationを使用して彼の回答(投稿を参照)で述べているように、私にとっては道でした。

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

def generate_data():
    # do calculations and stuff here
    return # an array reshaped(cols,rows) you want the color map to be  

def update(data):
    mat.set_data(data)
    return mat 

def data_gen():
    while True:
        yield generate_data()

fig, ax = plt.subplots()
mat = ax.matshow(generate_data())
plt.colorbar(mat)
ani = animation.FuncAnimation(fig, update, data_gen, interval=500,
                              save_count=50)
plt.show()
于 2012-06-30T15:31:58.570 に答える
1

np.histogram2d各ループ反復での使用であると思われます。または、ループの各ループ反復で、forクリアして新しい図を描画していること。スピードアップするには、Figure を 1 回作成し、その Figure のプロパティとデータをループで更新するだけです。これを行う方法については、matplotlib アニメーションの例を参照してください。通常、それmatplotlib.pyploy.plotには、ループ内での呼び出しaxes.set_xdata、およびの呼び出しが含まれaxes.set_ydataます。

ただし、あなたの場合は、matplotlib animation example dynamic image 2を見てください。この例では、データの生成はデータのアニメーションから分離されています (大量のデータがある場合、優れたアプローチではない可能性があります)。これら 2 つの部分を分割することで、どちらがボトルネックを引き起こしているか、numpy.histrogram2dまたはimshow(time.time()各部分の周りで使用) を確認できます。

Psnp.random.randnは疑似乱数ジェネレーターです。これらは、毎秒何百万もの (疑似) 乱数を生成できる単純な線形ジェネレーターである傾向があるため、これがボトルネックではないことはほぼ確実です。画面への描画は、ほとんどの場合、数値処理よりも遅いプロセスです。

于 2012-06-14T12:18:47.570 に答える