aha*_*jib 0 python matplotlib heatmap
我正在制作一个到目前为止看起来像这样的情节:
我使用以下代码:
import matplotlib.pyplot as plt
import numpy as np
column_labels = ['A','B']
row_labels = ['A','B']
data = np.random.rand(2,2)
print(type(data))
data = np.array([[1, 0.232], [0.119, 1]])
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.set_xticklabels(row_labels, minor=False)
ax.set_yticklabels(column_labels, minor=False)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我想要得到的是这样的:
我可以在哪里看到颜色条以及它们上的单元格的值.我需要在代码中添加什么来获取这些内容?
谢谢
对于颜色条,请参阅此示例。您猜对了,正确的函数调用就是ax.colorbar().
关于绘图上的值pcolor,恐怕您必须使用“手动”添加它们ax.text()。计算并循环单元格中心值并调用该text()函数。您可以使用关键字参数horizontalalignment='center'和verticalalignment='center'来保持文本值以 为中心x, y。
你看过seaborn了吗?
import seaborn as sns
data = np.random.rand(2,2)
sns.heatmap(data, annot=True, linewidths=.5)
Run Code Online (Sandbox Code Playgroud)