Cubius
Cubius

Reputation: 1314

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"

Upvotes: 3

Views: 4404

Answers (3)

Olivier Verdier
Olivier Verdier

Reputation: 49146

For 3D plotting in general, I would advise mayavi. It can be a bit daunting at first, but it is worth the effort.

It is certainly much faster than matplotlib for plotting one shot of 3D data. For plotting many times with a savefig call, I'm not sure...

Upvotes: 2

Jouni K. Seppänen
Jouni K. Seppänen

Reputation: 44118

I can't test if it helps since you didn't provide a runnable example, but you could see if modifying the Poly3DCollection returned by plot_surface is faster than creating a new collection every time.

Also, you are using a non-interactive backend, right? (Call matplotlib.use('agg') before importing pyplot.)

Upvotes: 0

tom10
tom10

Reputation: 69182

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.

Upvotes: 0

Related Questions