Python 更改子批次大小并使用histogram2d添加颜色栏

Python 更改子批次大小并使用histogram2d添加颜色栏,python,subplot,colorbar,histogram2d,Python,Subplot,Colorbar,Histogram2d,我正在尝试创建一个包含1行3列的图形,并且在很多事情上遇到了困难。首先,我的子绘图是非常小的碎片,我在尝试添加颜色条时出错。我怎样才能使子图成为方框,而颜色条显示在最右边 rmax0、rmax1和rmax2的范围都在0到700之间(大多数小于200),fspd的范围都在0到10之间 import matplotlib.pyplot as plt import numpy as np ax1 = plt.subplot(131) nice = plt.get_cmap('BuPu') bins1

我正在尝试创建一个包含1行3列的图形,并且在很多事情上遇到了困难。首先,我的子绘图是非常小的碎片,我在尝试添加颜色条时出错。我怎样才能使子图成为方框,而颜色条显示在最右边

rmax0、rmax1和rmax2的范围都在0到700之间(大多数小于200),fspd的范围都在0到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()
当我添加plt.colorbar()行时,我得到以下错误:
运行时错误:找不到可用于创建颜色栏的映射。首先定义一个可映射的对象,例如图像(使用imshow)或轮廓集(使用contourf)

你的图最终像一条细小的细条,因为你强迫纵横比。尝试禁用以下行:

ax1.set_aspect('equal')
ax2.set_aspect('equal')
ax3.set_aspect('equal')
如果您这样称呼它,则颜色栏将显示:

p = ax3.pcolormesh(X, Y, h, cmap=nice)
plt.colorbar(p, ax=[ax1, ax2, ax3])
我将所有轴的列表传递到这里的
colorbar
,以使colorbar收缩所有三个子批次以腾出空间。如果忽略此项,则仅从最后一个子地块中获取空间,使此子地块的宽度比其他子地块的宽度小

复制粘贴我用来测试此功能的完整代码:

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()

感谢您的帮助-我甚至没有在代码中看到set_方面行。我在colorbar命令中删除了ax=[ax1,ax2,ax3]部分,并将其绘制到第三个子图的右侧。如果我保留它,它会在第三个子图的正上方绘制颜色条。