简体   繁体   English

更改seaborn热图的颜色条上的刻度尺寸

[英]Change tick size on colorbar of seaborn heatmap

I want to increase the tick label size corresponding to the colorbar in a heatmap plot created using the seaborn module. 我想增加使用seaborn模块创建的热图图中与seaborn对应的刻度标签大小。 As an example: 举个例子:

import seaborn as sns
import pandas as pd
import numpy as np

arr = np.random.random((3,3))
df = pd.DataFrame(arr)
ax = sns.heatmap(arr)

Usually I would change the labelsize keyword using the tick_params method on a colorbar axes object, but with the heatmap() function I can only pass kwargs to the colorbar constructor. 通常我会在colorbar轴对象上使用tick_params方法更改labelsize关键字,但是使用tick_params heatmap()函数我只能将kwargs传递给colorbar构造函数。 How can I modify the tick label size for the colorbar in this plot? 如何在此图中修改颜色条的刻度标签大小?

Once you call heatmap the colorbar axes will get a reference at the axes attribute of the figure object. 调用heatmap ,色条轴将获得图形对象的axes属性的引用。 So you could either set up the figure ahead of time or get a reference to it after plotting with plt.gcf and then pull the colorbar axes object out that way: 因此,您可以提前设置图形,或者在使用plt.gcf绘图后获得对它的引用,然后以这种方式拉出plt.gcf轴对象:

import seaborn as sns
import pandas as pd
import numpy as np

arr = np.random.random((3,3))
df = pd.DataFrame(arr)
ax = sns.heatmap(arr)

cax = plt.gcf().axes[-1]
cax.tick_params(labelsize=20)

A slightly different way that avoids gcf() : 避免gcf()方式略有不同:

import seaborn as sns
import pandas as pd
import numpy as np

arr = np.random.random((3,3))
df = pd.DataFrame(arr)

fig, ax = plt.subplots()
sns.heatmap(arr, ax=ax)
ax.tick_params(labelsize=20)

I almost always start my plots this way, by explicitly creating a fig and ax object. 我几乎总是以这种方式开始我的情节,通过明确地创建一个figax对象。 It's a bit more verbose, but since I tend to forget my matplotlib-foo , I don't get confused with what I'm doing. 它有点冗长,但由于我倾向于忘记我的matplotlib-foo ,所以我不会对我正在做的事情感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM