回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在尝试制作一个交互式程序,它主要使用matplotlib绘制大量点(10k-100k左右)的散点图。现在它可以工作,但是更改需要很长时间才能呈现。少量的分数是可以的,但是一旦分数上升,事情就会很快变得令人沮丧。所以,我正在研究加速分散的方法,但我运气不太好</p>
<p>有一种显而易见的方法(现在的实现方式)
(我意识到情节重画没有更新。我不想用大量随机调用来改变fps结果)。</p>
<pre><code>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 )
</code></pre>
<p>速度达到每秒0.7帧</p>
<p>或者,我可以编辑散点返回的集合。为此,我可以改变颜色和位置,但不知道如何改变每个点的大小。我想应该是这样的</p>
<pre><code>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 )
</code></pre>
<p>这将导致0.7 fps的速度变慢。我想尝试使用CircleCollection或RegularPolygonCollection,因为这样可以很容易地更改大小,而且我不在乎更改标记。但是,我两个都画不出来,所以我不知道他们会不会画得更快。所以,现在我在找主意。</p>