python - 在 matplotlib 中绘制大量点和边

标签 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])

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

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/

相关文章:

python - 如何在 Pyplot 中获得平滑的平均曲线

python - Gtk* 后端需要安装 pygtk

python - 尝试在 SQLAlchemy 上运行插入语句时出现 CompileError

python - 我不明白 Python 中的 boolean 值

python - 如何求 NumPy 数组的元素模

python-2.7 - Python从数据中删除异常值

python - if else 条件在 df.loc pandas 内

python - Microsoft 合作伙伴生成 token

python - 如何在 np 数组中随机选择一个元素(仅当它不是某个值时)

python - 索引错误 : too many indices for array while plotting ROC curve with scikit-learn?