Python matplotlib insert index in plot

444 views Asked by At

So I am trying to save multiple plots which are generated after every iteration of a for loop and I want to insert a name tag on those plots like a header with the number of iterations done. code looks like this. I tried suptitle but it does not work.

for i in range(steps):

        nor_m = matplotlib.colors.Normalize(vmin = 0, vmax = 1)
        plt.hexbin(xxx,yyy,C, gridsize=13, cmap=matplotlib.cm.rainbow, norm=nor_m, edgecolors= 'k', extent=[-1,12,-1,12])
        plt.draw()
        plt.suptitle('frame'%i, fontsize=12)
        savefig("flie%d.png"%i)
1

There are 1 answers

0
David Zwicker On

What about plt.title?

for i in range(steps):

    nor_m = matplotlib.colors.Normalize(vmin=0, vmax=1)
    plt.hexbin(xxx, yyy, C, gridsize=13, cmap=matplotlib.cm.rainbow, norm=nor_m, edgecolors= 'k', extent=[-1,12,-1,12])
    plt.title('frame %d'%i, fontsize=12)
    plt.savefig("flie%d.png"%i)

You also had an error in the string formatting of the title call. Actually 'frame'%i should have failed with an TypeError: not all arguments converted during string formatting-error. Note also, that you don't need the plt.draw, since this will be called by plt.savefig.