seaborn clustermap:设置颜色条刻度 [英] seaborn clustermap: set colorbar ticks

查看:61
本文介绍了seaborn clustermap:设置颜色条刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改 seaborn.clustermap 中颜色条的刻度.

I would like to modify the ticks of a colorbar in a seaborn.clustermap. This answer addresses this question for a general matplotlib colorbar.

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

For some reason when I specify clustermap(..., vmin=0.25, vmax=1.0), I get ticks from 0.3 to 0.9, but no 1.0. If I extend vmax=1.05, I get a tick precisely at 1.05.

My guess was that the g.cax property of the object returned by clustermap is the colorbar, but it has no .set_ticks() method.

Any ideas how one can I set the ticks?

解决方案

Just like seaborn.heatmap the seaborn.clustermap has an argument cbar_kws (colorbar keyword arguments). This expects a dictionary of possible arguments to the matplotlib colorbar function. Because with matplotlib, we would use the ticks argument to colorbar in order to set the ticks manually, we can provide a dictionary like this

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

to obtain the tick marks at 0.25 and 1 in the colorbar. (The list can of course be extended, if you want more tickmarks.)

Complete code:

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

这篇关于seaborn clustermap:设置颜色条刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆