gpt4 book ai didi

python - 自定义注释 Seaborn 热图

转载 作者:太空狗 更新时间:2023-10-29 17:10:31 28 4
gpt4 key购买 nike

我在 Python 中使用 Seaborn 创建热图。我可以用传入的值对单元格进行注释,但我想添加注释来表示单元格的含义。例如,我不仅希望看到 0.000000,还希望看到相应的标签,例如“Foo”或 0.000000 (Foo)

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参数:

---------------------------------------------------------------------------
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}

希望一切都有意义,如果有人能提供任何帮助,我将不胜感激。

最佳答案

这个特性是在最新版本的 Seaborn 0.7.1 中刚刚添加的。

From Seaborn update history:

The annot parameter of heatmap() now accepts a rectangular dataset in addition to a boolean value. If a dataset is passed, its values will be used for the annotations, while the main dataset will be used for the heatmap cell colors

举个例子

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',这仅对数值有意义,并且会导致文本标 checkout 错。 enter image description here

关于python - 自定义注释 Seaborn 热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33158075/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com