简体   繁体   English

尝试在 Seaborn 热图中更改颜色条的文本颜色

[英]Trying to change the colorbar's text colour in a Seaborn Heatmap

I have two heatmap subplots using Seaborn (shown below) I have looked for tutorials/help etc everywhere but I cannot figure out:我有两个使用 Seaborn 的热图子图(如下所示) 我到处寻找教程/帮助等,但我无法弄清楚:

Q) How to change the color of the colorbar numbers on each of the heatmaps? Q) 如何更改每个热图上颜色条数字的颜色?

I want them both to be the color "yellow" and not the default "black"我希望它们都是“黄色”而不是默认的“黑色”

Thank you for you time.谢谢你的时间。

line_df
total_df
fig.set_facecolor("Blue")
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12,6))

sns.heatmap(line_df, ax = ax1, annot=True, annot_kws={'fontsize': 16, 'fontweight':'bold'}, xticklabels=line_df.columns, yticklabels=line_df.index, cbar_kws={'orientation':'vertical'} )

ax1.yaxis.label.set_color("Blue")
ax1.tick_params(colors="yellow")

sns.heatmap(total_df, ax = ax2, annot=True, annot_kws={'fontsize': 16, 'fontweight':'bold',}, xticklabels=total_df.columns, yticklabels=False, cbar_kws={'orientation':'vertical'})

ax2.get_yaxis().set_visible(False)
ax2.tick_params(colors="yellow")

fig.tight_layout()
plt.show()
plt.close()

You will need to use this to change the parameters including font color by calling each of the axis colorbar and then change the tick_params for that axis.您需要通过调用每个轴颜色条来使用它来更改包括字体颜色在内的参数,然后更改该轴的 tick_params。 As there was no data available, I have used random arrays to demonstrate the same.由于没有可用的数据,我使用随机 arrays 来演示相同的内容。 You can find more information tick_params here and on collections here您可以在这里找到更多信息 tick_params 和 collections在这里

df1 = np.random.rand(5, 5)
df2 = np.random.rand(5, 5)

import seaborn as sns

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12,6))

sns.heatmap(data = df2, ax = ax1, annot=True, annot_kws={'fontsize': 16, 'fontweight':'bold'}, 
           cbar_kws={'orientation':'vertical'} )
sns.heatmap(data = df1, ax = ax2, annot=True, annot_kws={'fontsize': 16, 'fontweight':'bold',}, 
           cbar_kws={'orientation':'vertical'})

cbar1 = ax1.collections[0].colorbar
cbar1.ax.tick_params(labelsize=20, colors='yellow')

cbar2 = ax2.collections[0].colorbar
cbar2.ax.tick_params(labelsize=20, colors='yellow')

plt.show()

在此处输入图像描述

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

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