Building a large number of points and edges in matplotlib

I have several points (about 3000) and edges (about 6000) in this 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 initial and final coordinates of each edge are specified:

 points[edges] 

I am looking for a faster / better way to build them. I currently 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') 

I read about lineCollections but don't know how to use them. Is there a way to use my data more directly? What is the bottleneck here?

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

 points = numpy.random.randint(0, 100, (3000, 2)) edges = numpy.random.randint(0, 3000, (6000, 2)) 
+3
source share
2 answers

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 possible to do it faster?

+3
source

You can also just do it in one story call, which is significantly faster than two (although probably pretty much the same as adding a 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() 
+2
source

Source: https://habr.com/ru/post/1398067/


All Articles