使用matplotlib的pyplot在每个单元格中带有文本的热图 [英] Heatmap with text in each cell with matplotlib's pyplot
本文介绍了使用matplotlib的pyplot在每个单元格中带有文本的热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 matplotlib.pyplot.pcolor()使用matplotlib绘制热图:
I use matplotlib.pyplot.pcolor() to plot a heatmap with matplotlib:
import numpy as np
import matplotlib.pyplot as plt
def heatmap(data, title, xlabel, ylabel):
plt.figure()
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap='RdBu', vmin=0.0, vmax=1.0)
plt.colorbar(c)
def main():
title = "ROC's AUC"
xlabel= "Timeshift"
ylabel="Scales"
data = np.random.rand(8,12)
heatmap(data, title, xlabel, ylabel)
plt.show()
if __name__ == "__main__":
main()
可以通过任何方式在每个单元格中添加相应的值,例如:
Is any way to add the corresponding value in each cell, e.g.:
(来自Matlab的可定制的热图)
(from Matlab's Customizable Heat Maps)
(尽管我很想知道将来的情况,但我目前的应用程序不需要其他的%
)
(I don't need the additional %
for my current application, though I'd be curious to know for the future)
推荐答案
您需要通过调用axes.text()
添加所有文本,下面是一个示例:
You need to add all the text by calling axes.text()
, here is an example:
import numpy as np
import matplotlib.pyplot as plt
title = "ROC's AUC"
xlabel= "Timeshift"
ylabel="Scales"
data = np.random.rand(8,12)
plt.figure(figsize=(12, 6))
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap='RdBu', vmin=0.0, vmax=1.0)
def show_values(pc, fmt="%.2f", **kw):
from itertools import izip
pc.update_scalarmappable()
ax = pc.get_axes()
for p, color, value in izip(pc.get_paths(), pc.get_facecolors(), pc.get_array()):
x, y = p.vertices[:-2, :].mean(0)
if np.all(color[:3] > 0.5):
color = (0.0, 0.0, 0.0)
else:
color = (1.0, 1.0, 1.0)
ax.text(x, y, fmt % value, ha="center", va="center", color=color, **kw)
show_values(c)
plt.colorbar(c)
输出:
这篇关于使用matplotlib的pyplot在每个单元格中带有文本的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文