Ben*_*min 4 python numpy matplotlib
我有一些这种格式的点(大约 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])
Run Code Online (Sandbox Code Playgroud)
其中边是点的索引,因此每条边的开始和结束坐标由下式给出:
points[edges]
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种更快/更好的方法来绘制它们。目前我有:
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')
Run Code Online (Sandbox Code Playgroud)
我读到了有关 lineCollections 的内容,但不确定如何使用它们。有没有办法更直接地使用我的数据?这里的瓶颈是什么?
一些更真实的测试数据,绘图时间约为132秒:
points = numpy.random.randint(0, 100, (3000, 2))
edges = numpy.random.randint(0, 3000, (6000, 2))
Run Code Online (Sandbox Code Playgroud)
好吧,我发现以下更快:
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')
Run Code Online (Sandbox Code Playgroud)
还有可能做得更快吗?
| 归档时间: |
|
| 查看次数: |
8084 次 |
| 最近记录: |