Tôi đang sử dụng Seaborn in Python để tạo bản đồ nhiệt. Tôi có thể chú thích các ô với các giá trị được truyền vào, nhưng tôi muốn thêm các chú thích biểu thị ý nghĩa của ô. Ví dụ: thay vì chỉ nhìn thấy 0.000000
, tôi muốn xem nhãn tương ứng, ví dụ: "Foo" hoặc 0.000000 (Foo)
.Chú thích tùy chỉnh Seaborn Heatmap
Các Seaborn documentation cho hàm Heatmap là một chút khó hiểu với tham số mà tôi tin là chìa khóa ở đây:
annot_kws : dict of key, value mappings, optional
Keyword arguments for ax.text when annot is True.
tôi đã cố gắng thiết lập annot_kws
để một cuốn từ điển của các bí danh đến các giá trị, tức là, {'Foo' : -0.231049060187, 'Bar' : 0.000000}
, v.v. nhưng tôi nhận được AttributeError.
Đây là mã của tôi (Tôi đã tự tạo ra các mảng dữ liệu vào đây để reproducability):
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)
Đây là (làm việc) đầu ra khi tôi không sử dụng các annot_kws
tham số:
Và đây stack trace cho khi tôi làm bao gồm 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
Cuối cùng, kws
, thuộc tính Tôi đang đi qua trong dòng trong ngăn xếp dấu vết, là từ điển và nó sẽ trông cơ bản như thế này:
kws = {'Foo': -0.231049060187, 'Bar': 0.0}
Hope tất cả mọi thứ có ý nghĩa, và tôi muốn đánh giá cao bất kỳ sự giúp đỡ nào mà mọi người có thể cung cấp
Bạn đã bao giờ quản lý để giải quyết vấn đề này chưa? – Tom
Thật không may .. – Tgsmith61591