Reputation: 689
I'm trying to create a figure with 1 row and 3 columns and struggling with many things. For one, my subplots turn out to be really tiny little slivers and I get an error when trying to add a colorbar. How can I make the subplots be boxes and the colorbar show up on the far right?
rmax0, rmax1, and rmax2 all range between 0 and 700 (majority are less than 200) and fspd ranges between 0 and 10.
import matplotlib.pyplot as plt
import numpy as np
ax1 = plt.subplot(131)
nice = plt.get_cmap('BuPu')
bins1 = np.arange(0,700,700/50.0)
bins2 = np.arange(0,10,10/50.0)
h,xedges,yedges = np.histogram2d(rmax0,fspd,bins=[bins1,bins2],normed=True)
X,Y = np.meshgrid(xedges,yedges)
ax1.pcolormesh(X, Y, h,cmap=nice)
ax1.set_aspect('equal')
plt.xlim([0,200])
plt.ylim([0,10])
ax2 = plt.subplot(132)
h,xedges,yedges = np.histogram2d(rmax1,fspd,bins=[bins1,bins2],normed=True)
X,Y = np.meshgrid(xedges,yedges)
ax2.pcolormesh(X, Y, h,cmap=nice)
ax2.set_aspect('equal')
plt.xlim([0,200])
plt.ylim([0,10])
ax3 = plt.subplot(133)
h,xedges,yedges = np.histogram2d(rmax2,fspd,bins=[bins1,bins2],normed=True)
X,Y = np.meshgrid(xedges,yedges)
ax3.pcolormesh(X, Y, h,cmap=nice)
ax3.set_aspect('equal')
plt.xlim([0,200])
plt.ylim([0,10])
plt.colorbar()
plt.show()
When I add the plt.colorbar() line I get the following error: RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).
Upvotes: 2
Views: 332
Reputation: 5409
Your plots end up like tiny little slivers because you force the aspect ratio. Try disabling these lines:
ax1.set_aspect('equal')
ax2.set_aspect('equal')
ax3.set_aspect('equal')
The colorbar will show if you call it like this:
p = ax3.pcolormesh(X, Y, h, cmap=nice)
plt.colorbar(p, ax=[ax1, ax2, ax3])
I pass a list of all axes to colorbar
here, to make the colorbar shrink all three subplots in order to make room. If you omit this, space will be taken from the last subplot only, making this subplot's width smaller than the others.
Copy-paste of the complete code I used to test this:
import matplotlib.pyplot as plt
import numpy as np
rmax0 = np.random.rand(1000) * 700
rmax1 = np.random.rand(1000) * 700
rmax2 = np.random.rand(1000) * 700
fspd = np.random.rand(1000) * 10
ax1 = plt.subplot(131)
nice = plt.get_cmap('BuPu')
bins1 = np.arange(0,700,700/50.0)
bins2 = np.arange(0,10,10/50.0)
h,xedges,yedges = np.histogram2d(rmax0,fspd,bins=[bins1,bins2],normed=True)
X,Y = np.meshgrid(xedges,yedges)
ax1.pcolormesh(X, Y, h,cmap=nice)
ax1.set_aspect('auto')
plt.xlim([0,200])
plt.ylim([0,10])
ax2 = plt.subplot(132)
h,xedges,yedges = np.histogram2d(rmax1,fspd,bins=[bins1,bins2],normed=True)
X,Y = np.meshgrid(xedges,yedges)
ax2.pcolormesh(X, Y, h,cmap=nice)
ax2.set_aspect('auto')
plt.xlim([0,200])
plt.ylim([0,10])
ax3 = plt.subplot(133)
h,xedges,yedges = np.histogram2d(rmax2,fspd,bins=[bins1,bins2],normed=True)
X,Y = np.meshgrid(xedges,yedges)
p = ax3.pcolormesh(X, Y, h,cmap=nice)
ax3.set_aspect('auto')
plt.xlim([0,200])
plt.ylim([0,10])
plt.colorbar(p, ax=[ax1, ax2, ax3])
plt.show()
Upvotes: 1