Finix
Finix

Reputation: 155

Anyway to draw many lines fast in python? Matploblib is preferred if possible

I have a geoJson file. A JSON type file and it has many points (387k) coordinates for 93k lines. I wonder if it's possible to use python plot all these lines into a figure fast, as I need to create many heat maps later for different time intervals.

Currently I'm using matplotlib

import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
# draw road network
# roadSegs structure: {road_segment1:[[line1_x, line1_y],...],...}
figure, ax = plt.subplots(1, 1)
ax.set_xlim(x_min - 100.0, x_max + 100.0)
ax.set_ylim(y_min - 100.0, y_max + 100.0)
for feature_id, lines in roadSegs.iteritems():
    for line in lines:
        ax.plot(line[0], line[1])
plt.savefig('road.png')

I need to use non-interactive backend as this code is going to be in the server. I try just drawing a single map. It has already taken 15mins and does not finish yet. Any idea to improve the speed? I know matplotlib is not designed for large dataset visualiation, but my dataset is less than 1M points. Any idea to improve its performance? If other libraries are recommended, which one should I use? At least it has to let me rendering lines and change the figure size.

Upvotes: 4

Views: 2486

Answers (1)

Finix
Finix

Reputation: 155

There is a section under pyplot tutorial that show to plot multiple lines, just do pyplot.plot(line1_x, line1_y, line2_x, line2_y,...). I simply change the code to call ax.plot() one time:

figure, ax = plt.subplots(1, 1)
ax.set_xlim(x_min - 100.0, x_max + 100.0)
ax.set_ylim(y_min - 100.0, y_max + 100.0)
drawLines = []
for feature_id, lines in roadSegs.iteritems():
    for line in lines:
        drawLines.append(line[0])
        drawLines.append(line[1])
ax.plot(*drawLines)
plt.savefig('road.png')

It works now. Taking about 4 minutes to draw my road network. Thanks to @ImportanceOfBeingErnest for pointing out my code's bottom-neck.

Upvotes: 8

Related Questions