2011-11-09 30 views
3

我有这种类型的格式的一些点(约3000)和边缘(6000):绘制大量的点和边缘的matplotlib

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)) 
+1

也许http://jbdeaton.com/2011/speed-up-plot-rendering-in-pythonmatplotlib/可能有一定的帮助。将plot()调用添加rasterized = True。 –

回答

3

好吧,我发现这是更快如下:

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()