【问题标题】:Plotting large numbers of points and edges in matplotlib在 matplotlib 中绘制大量点和边
【发布时间】:2011-12-25 11:22:21
【问题描述】:

我有一些点(大约 3000 个)和边缘(大约 6000 个)采用这种格式:

points = numpy.array([1,2],[4,5],[2,7],[3,9],[9,2])
edges = numpy.array([0,1],[3,4],[3,2],[2,4])

其中边是点的索引,因此每条边的起点和终点坐标由下式给出:

points[edges]

我正在寻找一种更快/更好的方法来绘制它们。目前我有:

from matplotlib import pyplot as plt
x = points[:,0].flatten()
y = points[:,1].flatten()
plt.plot(x[edges.T], y[edges.T], 'y-') # Edges
plt.plot(x, y, 'ro') # Points
plt.savefig('figure.png')

我阅读了有关 lineCollections 的信息,但不确定如何使用它们。有没有办法更直接地使用我的数据?这里的瓶颈是什么?

一些比较真实的测试数据,绘制时间约为132秒:

points = numpy.random.randint(0, 100, (3000, 2))
edges = numpy.random.randint(0, 3000, (6000, 2))

【问题讨论】:

标签: python numpy matplotlib


【解决方案1】:

嗯,我发现下面的方法要快得多:

from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
lc = LineCollection(points[edges])
fig = plt.figure()
plt.gca().add_collection(lc)
plt.xlim(points[:,0].min(), points[:,0].max())
plt.ylim(points[:,1].min(), points[:,1].max())
plt.plot(points[:,0], points[:,1], 'ro')
fig.savefig('full_figure.png')

还有没有可能做得更快?

【讨论】:

    【解决方案2】:

    您也可以在单个绘图调用中执行此操作,这比两次调用要快得多(尽管可能与添加 LineCollection 基本相同)。

    import numpy
    import matplotlib.pyplot as plt
    
    points = numpy.array([[1,2],[4,5],[2,7],[3,9],[9,2]])
    edges = numpy.array([[0,1],[3,4],[3,2],[2,4]])
    
    x = points[:,0].flatten()
    y = points[:,1].flatten()
    
    plt.plot(x[edges.T], y[edges.T], linestyle='-', color='y',
            markerfacecolor='red', marker='o') 
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2013-12-13
      • 2021-09-03
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      相关资源
      最近更新 更多