簡體   English   中英

Python-Seaborn:修改熱圖圖例

[英]Python - Seaborn: Modifying the heatmap legend

我剛剛創建了以下熱圖。 在此處輸入圖片說明

在圖例中,最大值(vmax)設置為0.10。 我這樣做是因為我想避免為更多的“極端”值着色。 但是,在圖例中,是否可以對其進行修改並寫入“> = 0.10”,所以添加“更大或等於”?

因此,這是一個非常hacky的解決方案,並且認為幾乎可以肯定有一種更聰明的方法,希望@mwaskom可以加入其中,但是我能夠通過在調用heatmap函數時將其作為參數顯式傳遞來訪問顏色欄對象,例如所以:

import seaborn as sns; sns.set()
import numpy as np; np.random.seed(0)
from matplotlib import pyplot as plt

fig, ax = plt.subplots()
fig.set_size_inches(14, 7)
uniform_data = np.random.rand(10, 12)
cbar_ax = fig.add_axes([.92, .3, .02, .4])
sns.heatmap(uniform_data, ax=ax, cbar_ax=cbar_ax)

產生這個:

在此處輸入圖片說明

我能夠在ax.get_yticks()找到刻度ax.get_yticks()

In [41]: cbar_ax.get_yticks()
Out [41]: array([ 0.19823662,  0.39918933,  0.60014204,  0.80109475])

標簽本身是字符串:

In [44]: [x.get_text() for x in cbar_ax.get_yticklabels()]
Out [44]: [u'0.2', u'0.4', u'0.6', u'0.8']

因此,我們可以簡單地在yticklabels中更改最后一個元素的文本對象,並希望得到校正后的軸,這是我的最終代碼:

fig, ax = plt.subplots()
fig.set_size_inches(14, 7)
uniform_data = np.random.rand(10, 12)
#add an axis to our plot for our cbar, tweak the numbers there to play with the sizing. 
cbar_ax = fig.add_axes([.92, .3, .02, .4])
#assign the cbar to be in that axis using the cbar_ax kw
sns.heatmap(uniform_data, ax=ax, cbar_ax=cbar_ax)

#hacky solution to change the highest (last) yticklabel
changed_val = ">= " + cbar_ax.get_yticklabels()[-1].get_text()

#make a new list of labels with the changed value.
labels = [x.get_text() for x in cbar_ax.get_yticklabels()[:-1]] + [changed_val]

#set the yticklabels to the new labels we just created. 
cbar_ax.set_yticklabels(labels)

產生:

在此處輸入圖片說明

關於此主題的一些其他資源可以在此處找到,我從mwaskom的響應中提取了一些信息。

暫無
暫無

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

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