python - 加速 matplotlib 散点图

标签 python matplotlib scatter

我正在尝试制作一个交互式程序,它主要使用 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,因为这可以让我轻松更改大小,而且我不关心更改标记。但是,我无法绘制,所以我不知道它们是否会更快。所以,在这一点上,我正在寻找想法。

最佳答案

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

  • 不同的标记类型
  • 限制色
  • 缩减数据集
  • 使用热图/网格代替散点图

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

关于python - 加速 matplotlib 散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18179928/

相关文章:

python - 通过 Python 在 Linux 上的进程列表

python - 如何在python中导入模块的一部分?

python - 从 FTP 检索文件时如何指定本地目标文件夹

python - Seaborn 错误?热图绘制不一致

python - Python 中 4D 向量的可视化

python - 如何在matplotlib中突出显示多条曲线中最低的曲线(包络线)

java - 如何以编程方式生成随机 JavaFX xyChart

java - 统计数据分析中的分散数据集

python - 保存使用 osmnx 生成的图形时,背景颜色不会保持蓝色

javascript - Python点击事件的Plotly(离线)