【问题标题】:seaborn clustermap: set colorbar ticksseaborn clustermap:设置颜色条刻度
【发布时间】:2017-05-19 18:02:13
【问题描述】:

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

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 属性是颜色条,但它没有.set_ticks() 方法。

任何想法如何设置刻度?

【问题讨论】:

  • 您的问题很清楚,但最好是写一个示例,以便有人可以轻松复制并粘贴它以开始帮助您。

标签: python matplotlib seaborn


【解决方案1】:

就像seaborn.heatmap 一样,seaborn.clustermap 有一个参数cbar_kws(颜色条关键字参数)。这需要 matplotlib colorbar 函数的可能参数字典。因为使用 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    相关资源
    最近更新 更多