【问题标题】:Changing subplot sizes and adding colorbar with histogram2d使用 histogram2d 更改子图大小并添加颜色条
【发布时间】:2015-04-18 16:38:15
【问题描述】:

我正在尝试创建一个具有 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() 行时,我收到以下错误: RuntimeError: 未找到可用于创建颜色条的可映射对象。首先定义一个可映射的对象,例如图像(使用 imshow)或轮廓集(使用 contourf)。

【问题讨论】:

    标签: python subplot colorbar histogram2d


    【解决方案1】:

    您的情节最终会像细小的碎片一样,因为您强制了纵横比。尝试禁用这些行:

    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,以使颜色条缩小所有三个子图以腾出空间。如果你忽略它,空间将只从最后一个子图中占用,使这个子图的宽度小于其他子图的宽度。

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

    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_aspect 行。我删除了 colorbar 命令中的 ax=[ax1,ax2,ax3] 部分,它绘制在我的第三个子图的右侧。如果我保留它,它会在第三个子图的顶部绘制颜色条。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2023-01-02
    • 1970-01-01
    • 2015-03-06
    • 2016-09-11
    • 2022-07-20
    相关资源
    最近更新 更多