The discussion of this thread was continued off the mailing list as the
example script/data were too large to post to the list.  Here is a
simplified summary so that others who find this thread will benefit too.

The original poster had a script like the following:

for i in range(48):
    fig = figure(...)
    ax = fig.add_subplot()
    ax.tripcolor(lons, lats, triangles, values, ...)
    # other plotting of coastlines, text, changing fig/ax settings, etc.
    fig.savefig(...)
    close(fig)

Three areas of performance improvement were identified:

1) As lons, lats and triangles do not change between tripcolor calls, use a
single Triangulation object initialised before the start of the loop.  This
avoids unnecessarily creating, using once and deleting 48 separate
Triangulation objects.

triangulation = matplotlib.tri.Triangulation(lons, lats, triangles)
for i in range(48):
    ....
    ax.tripcolor(triangulation, values, ...)
    ....

2) Use tricontourf instead of tripcolor.  It is often significantly faster,
and you can control the tradeoff of speed verses output quality by changing
the number of contour levels used.

3) Reuse the Figure and Axes within the loop.  Create them before the loop
and do all plotting that is common to all iterations of the loop.  Then in
the loop do the single tricontourf call.  You must make sure you delete the
result of the previous iteration's tricontourf call or they will all stack
up with each other as you repeat the loop.

This is the new pseudocode incorporating all 3 changes:

triangulation = matplotlib.tri.Triangulation(lons, lats, triangles)  # One
and only Triangulation.
fig = figure(...)  # One and only Figure.
ax = fig.add_subplot()  # One and only Axes.
# plotting of coastlines, text, changing fig/ax settings, etc.
ncollections = len(ax.collections)  # Number of collections added to Axes
so far.
for i in range(48):
    del ax.collections[ncollections:]  # Remove collections added in the
last loop iteration.
    ax.tricontourf(triangulation, values, ...)
    fig.savefig(...)
close(fig)

These changes reduced the execution time of the OP's code by about a factor
of 10 when using 16 levels for the tricontourf call.

Ian Thomas
------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
http://p.sf.net/sfu/logmein_12329d2d
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to