Matplotlib - A quick way to create many subheadings?

I use matplotlib to create a shape with many small subnets (something like 4 rows, 8 columns). I tried several different ways, and the fastest that I can get matplotlib to create axes is ~ 2 seconds. I saw this post about simply using one axis object to display many small images, but I would like to have ticks and names on the axes. Is there a way to speed up this process or make axes in matplotlib long enough?

Here is what I have tried so far:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import time

fig = plt.figure(figsize=(10,6))

plt.clf(); t = time.time()
grid = ImageGrid(fig, 111, 
                nrows_ncols = (4, 8))
print 'Time using ImageGrid: %.2f seconds'%(time.time()-t)

plt.clf(); t = time.time()
axes = plt.subplots(4,8)
print 'Time using plt.subplots: %.2f seconds'%(time.time()-t)

plt.clf(); t = time.time()
axes = []
for row in range(0,4):
    for col in range(1,9):
        ax = plt.subplot(4,8,row*8+col)
        axes.append(ax)
print 'Time using ptl.subplot loop: %.2f seconds'%(time.time()-t)

exit:

Time using ImageGrid: 4.25 seconds
Time using plt.subplots: 2.11 seconds
Time using ptl.subplot loop: 2.34 seconds

, , , , , script. :

import matplotlib.pyplot as plt
import pickle
import time

t = time.time()
fig,axes = plt.subplots(4,8,figsize=(10,6))
print 'Time using plt.subplots: %.2f seconds'%(time.time()-t)

pickle.dump((fig,axes),open('fig.p','wb'))

t = time.time()
fig,axes = pickle.load(open('fig.p','rb'))

print 'Time to load pickled figure: %.2f seconds'%(time.time()-t)

:

Time using plt.subplots: 2.01 seconds
Time to load pickled figure: 3.09 seconds
+4
1

subplots . - , 32 . .

? , , , - .

, (, image.set_data ..) , .

( , axes = plt.subplots(4, 8) . axes , . subplots . fig, axes = plt.subplots(...).)


:

"" . "", , "".

, / .

, , . , - :

import os
import cPickle as pickle
from functools import wraps

class Cached(object):
    def __init__(self, filename):
        self.filename = filename

    def __call__(self, func):
        @wraps(func)
        def new_func(*args, **kwargs):
            if not os.path.exists(self.filename):
                results = func(*args, **kwargs)
                with open(self.filename, 'w') as outfile:
                    pickle.dump(results, outfile, pickle.HIGHEST_PROTOCOL)
            else:
                with open(self.filename, 'r') as infile:
                    results = pickle.load(infile)
            return results
        return new_func

- (, utilities.py):

import matplotlib.pyplot as plt
from utilities import Cached

@Cached('temp.pkl')
def setup():
    fig, axes = plt.subplots(8, 4)
    # And perhaps some other data parsing/whatever
    return fig, axes

fig, axes = setup()
for ax in axes.flat:
    ax.plot(range(10))
plt.show()

, ("temp.pkl") , temp.pkl . , , matplotlib.

. , setup , "" ! , , .

, , . , .

+5

All Articles