[英]Python - Seaborn: Modifying the heatmap legend
因此,這是一個非常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.