加速matplotlib散点图

geo*_*rge 8 python scatter matplotlib

我正在尝试制作一个主要使用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 )
Run Code Online (Sandbox Code Playgroud)

这给出了一个快速的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 )
Run Code Online (Sandbox Code Playgroud)

这导致0.7 fps的速度变慢.我想尝试使用CircleCollection或RegularPolygonCollection,因为这样我可以轻松更改大小,而且我不关心更改标记.但是,我无法画画,所以我不知道他们是否会更快.所以,在这一点上,我正在寻找想法.

pel*_*son 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)。

HTH


Joh*_*yon 5

我已经经历了几次尝试加速散点图的大量积分,不同尝试:

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

这些都没有奏效.Matplotlib在分散图时效果不是很好.我唯一的建议是使用不同的绘图库,虽然我没有亲自找到一个合适的.我知道这没什么用,但它可以为你节省几个小时无果而终的修修补补.

  • 我真的希望那不是答案,matplotlib非常方便.你有没有机会提到你尝试过的一些不合适的matplotlib替换品,所以我不能花时间发现它们不起作用?现在,我要尝试的东西最重要的是chaco. (2认同)