Skip to content Skip to sidebar Skip to footer

Matplotlib Plotting In Loop, Removing Colorbar But Whitespace Remains

My code is something (roughly) like this: UPDATE: I've redone this with some actual mock-up code that reflects my general problem. Also, realized that the colorbar creation is in t

Solution 1:

colorbar() allows you explicitly set which axis to render into - you can use this to ensure that they always appear in the same place, and not steal any space from another axis. Furthermore, you could reset the .mappable attribute of an existing colorbar, rather than redefine it each time.

Example with explicit axes:

x = np.linspace(1,2, 100)
X, Y = np.meshgrid(x, x)
Z = plt.mlab.bivariate_normal(X,Y,1,1,0,0)

fig = plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.7])
ax2 = fig.add_axes([0.1,0.85,0.8,0.05])
...    

for i in range(1,5):
   plotted = ax1.pcolor(X,Y,Z)
   cbar = plt.colorbar(mappable=plotted, cax=ax2, orientation = 'horizontal')
   #note "cax" instead of "ax"
   plt.savefig(os.path.expanduser(os.path.join('~/', str(i))))
   plt.draw()

Solution 2:

I had a very similar problem, which I finally managed to solve by defining a colorbar axes in a similar fashion to: Multiple imshow-subplots, each with colorbar

The advantage compared to mdurant's answer is that it saves defining the axes location manually.

import matplotlib.pyplot as plt
import IPython.display as displayfrom mpl_toolkits.axes_grid1 import make_axes_locatable
from pylab import *
%matplotlib inline

def plot_res(ax,cax):
    plotted=ax.imshow(rand(10, 10))
    cbar=plt.colorbar(mappable=plotted,cax=cax)

fig, axarr = plt.subplots(2, 2)
cax1 = make_axes_locatable(axarr[0,0]).append_axes("right", size="10%", pad=0.05)
cax2 = make_axes_locatable(axarr[0,1]).append_axes("right", size="10%", pad=0.05)
cax3 = make_axes_locatable(axarr[1,0]).append_axes("right", size="10%", pad=0.05)
cax4 = make_axes_locatable(axarr[1,1]).append_axes("right", size="10%", pad=0.05)
# plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0.3, hspace=0.3)
N=10
for j in range(N):
    plot_res(axarr[0,0],cax1)
    plot_res(axarr[0,1],cax2)
    plot_res(axarr[1,0],cax3)
    plot_res(axarr[1,1],cax4)
    display.clear_output(wait=True)
    display.display(plt.gcf())
display.clear_output(wait=True)

Post a Comment for "Matplotlib Plotting In Loop, Removing Colorbar But Whitespace Remains"