10

主にmatplotlibを使用して、かなり多くのポイント(10k-100k程度)の散布図を作成するインタラクティブなプログラムを作成しようとしています。現在は機能していますが、変更のレンダリングに時間がかかりすぎます。点数が少ないのはいいのですが、点数が上がると急にイライラしてきます。だから、私はスキャッターをスピードアップする方法に取り組んでいますが、あまり運がありません

明らかな方法があります(現在実装されている方法)(更新せずにプロットが再描画されることに気付きました。ランダムへの大きな呼び出しでfpsの結果を変更したくありませんでした)。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import time


X = np.random.randn(10000)  #x pos
Y = np.random.randn(10000)  #y pos
C = np.random.random(10000) #will be color
S = (1+np.random.randn(10000)**2)*3 #size

#build the colors from a color map
colors = mpl.cm.jet(C)
#there are easier ways to do static alpha, but this allows 
#per point alpha later on.
colors[:,3] = 0.1

fig, ax = plt.subplots()

fig.show()
background = fig.canvas.copy_from_bbox(ax.bbox)

#this makes the base collection
coll = ax.scatter(X,Y,facecolor=colors, s=S, edgecolor='None',marker='D')

fig.canvas.draw()

sTime = time.time()
for i in range(10):
    print i
    #don't change anything, but redraw the plot
    ax.cla()
    coll = ax.scatter(X,Y,facecolor=colors, s=S, edgecolor='None',marker='D')
    fig.canvas.draw()
print '%2.1f FPS'%( (time.time()-sTime)/10 )

これにより、0.7 fps が高速になります

または、スキャッターによって返されたコレクションを編集することもできます。そのために、色と位置を変更できますが、各ポイントのサイズを変更する方法がわかりません。それは私がこのように見えると思います

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import time


X = np.random.randn(10000)  #x pos
Y = np.random.randn(10000)  #y pos
C = np.random.random(10000) #will be color
S = (1+np.random.randn(10000)**2)*3 #size

#build the colors from a color map
colors = mpl.cm.jet(C)
#there are easier ways to do static alpha, but this allows 
#per point alpha later on.
colors[:,3] = 0.1

fig, ax = plt.subplots()

fig.show()
background = fig.canvas.copy_from_bbox(ax.bbox)

#this makes the base collection
coll = ax.scatter(X,Y,facecolor=colors, s=S, edgecolor='None', marker='D')

fig.canvas.draw()

sTime = time.time()
for i in range(10):
    print i
    #don't change anything, but redraw the plot
    coll.set_facecolors(colors)
    coll.set_offsets( np.array([X,Y]).T )
    #for starters lets not change anything!
    fig.canvas.restore_region(background)
    ax.draw_artist(coll)
    fig.canvas.blit(ax.bbox)
print '%2.1f FPS'%( (time.time()-sTime)/10 )

これにより、0.7 fps が遅くなります。CircleCollection または RegularPolygonCollection を使用してみました。これにより、サイズを簡単に変更でき、マーカーの変更は気にしません。しかし、どちらも描くことができないので、どちらが速いかわかりません。ということで、現在アイデア募集中です。

4

2 に答える 2

7

We are actively working on performance for large matplotlib scatter plots. I'd encourage you to get involved in the conversation (http://matplotlib.1069221.n5.nabble.com/mpl-1-2-1-Speedup-code-by-removing-startswith-calls-and-some-for-loops-td41767.html) and, even better, test out the pull request that has been submitted to make life much better for a similar case (https://github.com/matplotlib/matplotlib/pull/2156).

HTH

于 2013-08-15T20:33:14.563 に答える