remove colorbar from figure in matplotlib

后端 未结 9 551
终归单人心
终归单人心 2020-12-08 14:14

This should be easy but I\'m having a hard time with it. Basically, I have a subplot in matplotlib that I\'m drawing a hexbin plot in every time a function is called, but ev

9条回答
  •  一整个雨季
    2020-12-08 14:59

    I needed to remove colorbars because I was plotting a pcolormesh and adding colorbar to a figure in a loop. Each loop would create a new colorbar and after ten loops I would have ten colorbars. That was bad.

    To remove colorbars, I name the pcolormesh and colorbar a variable, then at the end of my loop I remove each. It is important to remove the colorbar before removing the pcolormesh.

    Psudo Code:

     for i in range(0,10):
       p = plt.pcolormesh(datastuff[i])
       cb = plt.colorbar(p)
       plt.savefig('name_'+i)
    
       cb.remove()
       p.remove()
    

    Again, it was necessary to remove the colorbar before the pcolormesh

提交回复
热议问题