我有一些点(大约 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))
最佳答案
好吧,我发现下面的速度要快得多:
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')
还有可能做得更快吗?
关于python - 在 matplotlib 中绘制大量点和边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8068183/