簡體   English   中英

更改Seaborn熱圖顏色條的高度

[英]Change the height of a Seaborn heatmap colorbar

我有以下Python代碼使用Seaborn包創建熱圖:

f, ax = plt.subplots(figsize=(21,5))
heatmap = sns.heatmap(significantBig5[basic].as_matrix().T,mask=labelsDf.T,
     annot=False,fmt='.2f',yticklabels=basic_labels,linewidths=0.5,square=True,
     xticklabels=traits)
plt.xticks(rotation=70)

這將創建以下熱圖: 樣本熱圖

我想格式化此熱圖,以便右側的顏色條更短。 有沒有辦法做到這一點?

您還可以使用cns熱圖顏色欄cbar_kws的額外關鍵字參數。 他們修改了此處記錄的matplotlib顏色條的設置

使用@Serenity的代碼和shrinkage關鍵字,我們得到這個:

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pylab as plt

arr = np.random.random((1,9))
df = pd.DataFrame(arr)
sns.heatmap(arr,cbar=True,cbar_kws={"shrink": .82})

plt.show()

在此輸入圖像描述

使用cbar_ax參數並設置你喜歡的顏色條的位置:

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pylab as plt

arr = np.random.random((1,9))
df = pd.DataFrame(arr)
fig, ax = plt.subplots(1, 1)
cbar_ax = fig.add_axes([.905, .3, .05, .3])
sns.heatmap(arr, ax=ax, cbar_ax = cbar_ax, cbar=True)

plt.show()

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM