Pyplot: really slow heatmap creation

I have a loop that executes the body about 200 times. In each iteration of the loop, it performs a complex calculation, and then as a debug, I want to create a heat map of the NxM matrix. But generating this heatmap is unbearably slow and significantly slows down the slow algorithm.

My code is line by line:

import numpy
import matplotlib.pyplot as plt
for i in range(200):
    matrix = complex_calculation()
    plt.set_cmap("gray")
    plt.imshow(matrix)
    plt.savefig("frame{0}.png".format(i))

The matrix, from numpy, is not huge --- 300 x 600 doubles. Even if I don’t save the number and instead update the screen graph, it’s even slower.

Of course I have to abuse the pipot. (Matlab can do this, no problem.) How do I speed this up?

+5
source share
2 answers

plt.clf() , :

for i in range(200):
    matrix = complex_calculation()
    plt.set_cmap("gray")
    plt.imshow(matrix)
    plt.savefig("frame{0}.png".format(i))
    plt.clf()

, , .

+5

, :

import matplotlib.pyplot as plt
from matplotlib import cm
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
for i in range(200):
    matrix = complex_calculation()
    ax.imshow(matrix, cmap=cm.gray)
    fig.savefig("frame{0}.png".format(i))

plt.imshow gca, gcf, , ; , . , .

+3

All Articles