Cubius Cubius - 1 month ago 14
Python Question

How do I increase the speed of plotting in python?

I have a 256x256 numpy-array of data which is constantly being changed. on every iteration I take a snapshot to make a movie. snapshot is a 3d surface plot made using

matplotlib
.

The problem is that plotting costs me >2 seconds on every iteration which is about 600 seconds for 250 iterations. I had the same program running in MATLAB and it was 80-120 seconds for the same number of iterations.

The question: are there ways to speed up
matplotlib
3d surface plotting or are there faster plotting tools for python?

Here is some of the code:

## initializing plot

fig = plt.figure(111)
fig.clf()
ax = fig.gca(projection='3d')
X = np.arange(0, field_size, 1)
Y = np.arange(0, field_size, 1)
X, Y = np.meshgrid(X, Y)

## the loop

start_time = time.time()
for k in xrange(250):
it_time = time.time()
field[128,128] = maxvalue
field = scipy.ndimage.convolve(field, kernel)
print k, " calculation: ", time.time() - it_time, " seconds"
it_time = time.time()
ax.cla()
ax.plot_surface(X, Y, field.real, rstride=4, cstride=4, cmap=cm.hot,
linewidth=0, antialiased=False)
ax.set_zlim3d(-50, 150)
filename = "out_%d.png" % k
fig.savefig(filename)
#fig.clf()
print k, " plotting: ", time.time() - it_time, " seconds"
print "computing: ", time.time() - start_time, " seconds"

Answer

GNUplot (accessed through it's various python interfaces) may be faster. At least I knew someone a few years ago with a similar problem to yours and after testing a number of packages they went with GNUplot. It's not nearly as good looking as matplotlib though.

Also, I assume you have interactive mode turned off.

Comments