Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python colorbar: how to stop its repeating in for loop

I wrote a for-loop to generate a set of images:

for p in range(0,153):
im=plt.imshow(M[p,0:28,:].T,extent=[0,time.max(),depth.max(),0],aspect='auto')
plt.tight_layout()
cbar=plt.colorbar(im,orientation='vertical')
cbar.set_label('Vz[mm]')
plt.title('Rohdaten '+str(p)+'. Umlauf D874 Ch5')
plt.xlabel('Messzeit[s]')
plt.ylabel('Messtiefe[mm]')
savefig(os.path.join('/Users/gaoyingqiang/Desktop/1-153Umlauf',str(p)+'.png'))

But it turns the colorbar in the images was looping like: weird isn't it?

I cannot understand why the colorbar was repeating itself.

like image 634
Yingqiang Gao Avatar asked Mar 24 '17 13:03

Yingqiang Gao


1 Answers

If I interprete the incomplete code correctly, you want to save different figures, each with a colorbar.

  1. You could actually create different figures and not operate on the same figure over and over again by closing the old figure, plt.close("all").
  2. You could clear the old figure using plt.clf(), thereby operating on the same but emptied figure.

Example:

for p in range(0,153):

    im=plt.imshow(M[p,0:28,:].T,extent=[0,time.max(),depth.max(),0],aspect='auto')
    plt.tight_layout()
    cbar=plt.colorbar(im,orientation='vertical')
    cbar.set_label('Vz[mm]')
    plt.title('Rohdaten '+str(p)+'. Umlauf D874 Ch5')
    plt.xlabel('Messzeit[s]')
    plt.ylabel('Messtiefe[mm]')
    savefig(os.path.join('/Users/gaoyingqiang/Desktop/1-153Umlauf',str(p)+'.png'))
    plt.close("all")
like image 167
ImportanceOfBeingErnest Avatar answered Oct 14 '22 05:10

ImportanceOfBeingErnest