在matplotlib中绘制大量的点和边 [英] Plotting large numbers of points and edges in matplotlib

查看:197
本文介绍了在matplotlib中绘制大量的点和边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some points (about 3000) and edges (about 6000) in this type of format:

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

其中边缘是指向点的索引,因此每个边缘的开始和结束坐标由下式给出:

where the edges are indices into points, so that the start and end coordinates of each edges is given by:

points[edges]

我正在寻找绘制它们的更快/更好的方法.目前,我有:

I am looking for a faster / better way to plot them. Currently I have:

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,但不确定如何使用它们.有没有一种方法可以更直接地使用我的数据?这里的瓶颈是什么?

I read about lineCollections, but am unsure how to use them. Is there a way to use my data more directly? What is the bottleneck here?

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

Some more realistic test data, time to plot is about 132 seconds:

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

推荐答案

好吧,我发现以下内容要快得多:

Well, I found the following which is much faster:

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

是否还有可能做得更快?

Is it still possible to do it faster?

这篇关于在matplotlib中绘制大量的点和边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆