Benjamin
Benjamin

Reputation: 11860

Plotting large numbers of points and edges in matplotlib

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

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?

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

Upvotes: 4

Views: 8655

Answers (2)

Joe Kington
Joe Kington

Reputation: 284622

You could also just do it in a single plot call, which is considerably faster than two (though probably essentially 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()

Upvotes: 3

Benjamin
Benjamin

Reputation: 11860

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?

Upvotes: 5

Related Questions