首页 文章

Seaborn热图改变了colorbar的大小

提问于
浏览
1

使用以下代码,我将一个seaborn热图与一个颜色条一起绘制 . 我想将colorbar的大小设置为等于heatmap的大小 . 我怎样才能做到这一点?

我试图使用 fig.colorbar(heatmap) 来修改颜色栏,但这会返回错误:

AttributeError: 'AxesSubplot' object has no attribute 'autoscale_None'

码:

fig,ax=plt.subplots(figsize=(30,60))

cmap = plt.get_cmap('inferno',30)
cmap.set_under('white')#Colour values less than vmin in white
cmap.set_over('yellow')# colour valued larger than vmax in red 

Crosstab=50000*np.random.randn(10,10)

heatmap=sns.heatmap(Crosstab[::-1],cmap=cmap,annot=False,square=True,ax=ax,vmin=1,vmax=50000,linewidths=0.8,linecolor="grey")



plt.show()

enter image description here

1 回答

  • 3

    如评论中所述,我无法使用Seaborn版本0.8和matplotlib 2.1.1重现此问题,因此如果可能,我建议更新模块 .

    话虽这么说,你可以使用seaborn.heatmap中的 cbar_kws 参数来操纵颜色条的大小 . 这需要是一个字典,通过(在引擎盖下)作为kwargs传递给matplotlibs fig.colorbar() .

    一个感兴趣的是 shrink 参数 . 这会缩小颜色条的大小:

    收缩:1.0;用于乘以颜色条大小的分数

    默认值应为1.0,因此您可以尝试手动将其设置为1.但是,如果这不起作用,您可以使用较低的值来缩小颜色条 . 这可能需要一些修补才能使色条尺寸合适 .

    fig, ax = plt.subplots()
    
    cmap = plt.get_cmap('inferno',30)
    cmap.set_under('white')#Colour values less than vmin in white
    cmap.set_over('yellow')# colour valued larger than vmax in red
    
    Crosstab=50000*np.random.randn(10,10)
    
    heatmap=sns.heatmap(Crosstab[::-1],cmap=cmap,annot=False,square=True,ax=ax,vmin=1,vmax=50000,
                        cbar_kws={"shrink": 0.5},linewidths=0.8,linecolor="grey")
    
    plt.show()
    

    赠送:

    enter image description here

相关问题