10

我正在尝试制作一个交互式程序,该程序主要使用 matplotlib 来制作相当多点(10k-100k 左右)的散点图。现在它可以工作,但是更改需要很长时间才能呈现。少量的点是可以的,但是一旦数量增加,事情就会很快令人沮丧。所以,我正在研究加快分散速度的方法,但我运气不佳

有一种明显的做事方式(现在实现的方式)(我意识到情节在没有更新的情况下重绘。我不想通过大量调用 random 来改变 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

或者,我可以编辑 scatter 返回的集合。为此,我可以更改颜色和位置,但不知道如何更改每个点的大小。那我认为看起来像这样

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

我已经经历过几次尝试加速具有大量点的散点图,各种尝试:

  • 不同的标记类型
  • 限制颜色
  • 削减数据集
  • 使用热图/网格而不是散点图

这些东西都没有奏效。Matplotlib 在散点图方面表现不佳。我唯一的建议是使用不同的绘图库,尽管我个人还没有找到合适的。我知道这没有多大帮助,但它可能会为您节省几个小时徒劳的修修补补。

于 2013-08-12T05:45:53.373 回答
7

我们正在积极研究大型 matplotlib 散点图的性能。我鼓励您参与对话(http://matplotlib.1069221.n5.nabble.com/mpl-1-2-1-Speedup-code-by-removing-startswith-calls-and-some- for-loops-td41767.html),甚至更好的是,测试已提交的拉取请求,以使类似案例的生活变得更好(https://github.com/matplotlib/matplotlib/pull/2156)。

高温高压

于 2013-08-15T20:33:14.563 回答