2015-10-15 140 views
8

我在Python中使用Seaborn創建一個Heatmap。我可以使用傳入的值對單元格進行註釋,但我想添加註釋來表示單元格的含義。例如,我不希望看到0.000000,而是希望看到相應的標籤,例如「Foo」或0.000000 (Foo)自定義註釋Seaborn熱圖

Seaborn documentation的熱圖功能是有點神祕與我相信是這裏的關鍵參數:

annot_kws : dict of key, value mappings, optional 
    Keyword arguments for ax.text when annot is True. 

我試着設置annot_kws的別名的字典中的值,即{'Foo' : -0.231049060187, 'Bar' : 0.000000},等等,但我得到一個AttributeError。

這裏是我的代碼(我手動創建這裏的數據數組重現性):

data = np.array([[0.000000,0.000000],[-0.231049,0.000000],[-0.231049,0.000000]]) 
axs = sns.heatmap(data, vmin=-0.231049, vmax=0, annot=True, fmt='f', linewidths=0.25) 

這裏是(工作)輸出,當我不使用annot_kws參數:

Working output

在這裏,當我堆棧跟蹤包括annot_kws PARAM:

--------------------------------------------------------------------------- 
AttributeError       Traceback (most recent call last) 
<ipython-input-57-38f91f1bb4b8> in <module>() 
    12 
    13 
---> 14 axs = sns.heatmap(data, vmin=min(uv), vmax=max(uv), annot=True, annot_kws=kws, linewidths=0.25) 
    15 concepts 

/opt/anaconda/2.3.0/lib/python2.7/site-packages/seaborn/matrix.pyc in heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, ax, xticklabels, yticklabels, mask, **kwargs) 
    272  if square: 
    273   ax.set_aspect("equal") 
--> 274  plotter.plot(ax, cbar_ax, kwargs) 
    275  return ax 
    276 

/opt/anaconda/2.3.0/lib/python2.7/site-packages/seaborn/matrix.pyc in plot(self, ax, cax, kws) 
    170   # Annotate the cells with the formatted values 
    171   if self.annot: 
--> 172    self._annotate_heatmap(ax, mesh) 
    173 
    174   # Possibly add a colorbar 

/opt/anaconda/2.3.0/lib/python2.7/site-packages/seaborn/matrix.pyc in _annotate_heatmap(self, ax, mesh) 
    138    val = ("{:" + self.fmt + "}").format(val) 
    139    ax.text(x, y, val, color=text_color, 
--> 140      ha="center", va="center", **self.annot_kws) 
    141 
    142  def plot(self, ax, cax, kws): 

/opt/anaconda/2.3.0/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in text(self, x, y, s, fontdict, withdash, **kwargs) 
    590   if fontdict is not None: 
    591    t.update(fontdict) 
--> 592   t.update(kwargs) 
    593   self.texts.append(t) 
    594   t._remove_method = lambda h: self.texts.remove(h) 

/opt/anaconda/2.3.0/lib/python2.7/site-packages/matplotlib/artist.pyc in update(self, props) 
    755    func = getattr(self, 'set_' + k, None) 
    756    if func is None or not six.callable(func): 
--> 757     raise AttributeError('Unknown property %s' % k) 
    758    func(v) 
    759    changed = True 

AttributeError: Unknown property tokenized 

最後,kws,我傳遞的堆棧跟蹤行的屬性,是字典,它看起來基本上是這樣的:

kws = {'Foo': -0.231049060187, 'Bar': 0.0} 

希望一切是有道理的,而且我感謝任何人可以給予的幫助。

+0

你有沒有管理來解決這個問題? – Tom

+0

不幸的是不是.. – Tgsmith61591

回答

12

該功能剛添加到最新版本的Seaborn 0.7.1中。

Seaborn update history:熱圖的

的ANNOT參數()現在接受除了一個布爾值的矩形數據集。如果一個數據集被傳遞,它的值將被用於說明,而主數據集將被用於熱圖單元顏色

下面是一個例子

data = np.array([[0.000000,0.000000],[-0.231049,0.000000],[-0.231049,0.000000]]) 
labels = np.array([['A','B'],['C','D'],['E','F']]) 
fig, ax = plt.subplots() 
ax = sns.heatmap(data, annot = labels, fmt = '') 

筆記,FMT =如果您使用非數字標籤,''是必要的,因爲默認值是fmt ='。2g',這對數字值是有意義的,並且會導致文本標籤出錯。 enter image description here

2

aanot_kws在Seaborn用於不同的用途,即,它提供了訪問註釋的顯示方式,而不是所顯示的內容

import matplotlib.pyplot as plt 
import seaborn as sns 

sns.set() 
fig, ax = plt.subplots(1,2) 
ata = np.array([[0.000000,0.000000],[-0.231049,0.000000],[-0.231049,0.000000]]) 
sns.heatmap(data, vmin=-0.231049, vmax=0, annot=True, fmt='f', annot_kws={"size": 15}, ax=ax[0]) 
sns.heatmap(data, vmin=-0.231049, vmax=0, annot=True, fmt='f', annot_kws={"size": 10}, ax=ax[1]); 

enter image description here

+0

謝謝@bushmanov。你知道有什麼方法來改變註釋嗎?或者這是一個失敗的原因? – Tgsmith61591

+0

@ Tgsmith61591感謝您的接受。熱圖是值的表示,所以我強烈相信*熱圖沒有固有的方法可以從底層數據中拉出不存在的標籤'Foo'。如果你確實需要附上一個標籤,我會看看matplotlib如何在其圖上疊加文本。畢竟seaborn是matplotlib。 –

3

我不相信這在當前版本中是可能的。如果你是到一個黑客-Y的解決方法,你可以做以下...

# Create the 1st heatmap without labels 
sns.heatmap(data=df1, annot=False,) 

# create the second heatmap, which contains the labels, 
# turn the annotation on, 
# and make it transparent 
sns.heatmap(data=df2, annot=True, alpha=0.0) 

注意,你可能有一個問題,您的文字標籤的顏色。在這裏,我創建了一個自定義cmap,使所有標籤均勻黑色。

+0

這就是我一直在尋找的東西。你可以添加一個自定義配色方案的例子嗎? –