2017-05-19 224 views
4

我想修改seaborn.clustermap中颜色条的刻度。 This answer解决了这个问题的一般matplotlib彩条。seaborn clustermap:设置颜色条刻度

g = sns.clustermap(np.random.rand(20,20), 
        row_cluster=None, col_cluster=None, 
        vmin = 0.25, vmax=1.0) 

出于某种原因,当我指定clustermap(..., vmin=0.25, vmax=1.0),我得到蜱从0.3到0.9,但没有1.0。如果我扩展vmax=1.05,我会在1.05处得到一个刻度。

我的猜测是clustermap返回的对象的g.cax属性是colorbar,但它没有.set_ticks()方法。

任何想法如何设置蜱?

+2

你的问题是明确的,但如果你写了这样一个例子这将是最好的有人可以轻松地复制和粘贴它开始帮助你。 – mwaskom

回答

5

就像seaborn.heatmapseaborn.clustermap有一个参数cbar_kws(colorbar关键字参数)。这需要matplotlib彩条功能可能参数的字典。因为与matplotlib,我们将使用ticks参数以手动设置刻度线以彩条,我们可以提供这样

g = sns.clustermap(..., cbar_kws={"ticks":[0.25,1]}) 

一本字典在彩条0.251获得刻度线。 (当然,清单可以延长,如果你想要更多的刻度线。)

完整代码:

import seaborn as sns 
import matplotlib.pyplot as plt 
import numpy as np 

g = sns.clustermap(np.random.rand(20,20), 
        row_cluster=None, col_cluster=None, 
        vmin = 0.25, vmax=1.0, cbar_kws={"ticks":[0.25,1]}) 

plt.show() 

enter image description here