import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4,4)
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.set_label_position('top') # <-- This doesn't work!
ax.set_xticklabels(row_labels, minor=False)
ax.set_yticklabels(column_labels, minor=False)
plt.show()
以上代码来自:Moving x-axis to the top of a plot in matplotlib
如何更改此脚本的输出,使其在美学上看起来更像以下图片:
任何使用python matplotlib或seborn的解决方案都可以工作。我想在单元格之间插入白色,让单元格成为正方形,并控制它们的大小
1条答案
按热度按时间vcirk6k61#
我想你需要两个技巧。第一,添加行
使单元格显示为正方形(假设x轴和y轴上的数字相等,如示例所示)。如果x轴上有x个正方形,y轴上有y个正方形,我想您可以这样做,
第二,你需要给单元格添加edgecolor,并使边缘变厚,所以修改你的线条,例如,
结果是