我有一个Subplots
带有混淆矩阵,该矩阵与HeatMap
一起显示。
1)在“目标”列上方添加一个大标题
2)添加一个大的Ylabel“预测”
3)每列只有一个大的图例,因为它们显示的是同一件事
4)为每一列添加列名
['Train CM', 'Train Norm CM', 'Validation CM', 'Validation Norm CM']
和行名[f'Epoch {i}' for i in range(n_epoch)]
。我在here中喜欢
但仅适用于列而不适用于行,我不知道为什么。
我的代码:
cols = ['Train CM', 'Train Norm CM', 'Validation CM', 'Validation Norm CM']
rows = [f'Epoch {i}' for i in range(n_epoch)]
f, axes = plt.subplots(nrows = n_epoch, ncols = 4, figsize=(40, 30))
for ax, col in zip(axes [0], cols):
ax.set_title(col, size='large')
for ax, row in zip(axes[:,0], rows):
ax.set_ylabel(row, rotation=0, size='large')
f.tight_layout()
for e in range(n_epoch):
for c in range(4):
# take conf matrix from lists cm_Train or cm_Validation of ConfusionMatrix() objects
if c == 0:
cm = np.transpose(np.array([list(item.values()) for item in cm_Train[e].matrix.values()]))
elif c == 1:
cm = np.transpose(np.array([list(item.values()) for item in cm_Train[e].normalized_matrix.values()]))
elif c == 2:
cm = np.transpose(np.array([list(item.values()) for item in cm_Validation[e].matrix.values()]))
else:
cm = np.transpose(np.array([list(item.values()) for item in cm_Validation[e].normalized_matrix.values()]))
sns.heatmap(cm, annot=True, fmt='g', ax = axes[e, c], linewidths=.3)
答案 0 :(得分:1)
由于我没有您的数据,因此我提出的解决方案具有空图。这是您想要的吗?
n_epoch = 4
cols = ['Train CM', 'Train Norm CM', 'Validation CM', 'Validation Norm CM']
rows = [f'Epoch {i}' for i in range(n_epoch)]
f, axes = plt.subplots(nrows = n_epoch, ncols = 4, figsize=(12, 8))
f.text(0, 0.5, 'Predictions', ha='center', va='center', fontsize=20, rotation='vertical')
plt.suptitle("One big title", fontsize=18, y=1.05)
for ax, col in zip(axes [0], cols):
ax.set_title(col, size='large')
for ax, row in zip(axes[:, 0], rows):
ax.set_ylabel(row, size='large')
plt.tight_layout()
放置颜色条:在这里,您将颜色条放置在每一列的所有行上。但是,这里的tight_layout()
不兼容,因此您必须将其关闭。
f, axes = plt.subplots(nrows = n_epoch, ncols = 4, figsize=(12, 8))
for i, ax in enumerate(axes.flat):
im = ax.imshow(np.random.random((20,20)), vmin=0, vmax=1)
if i%4 == 0:
f.colorbar(im, ax=axes[:,int(i/4)].ravel().tolist(), aspect=30, pad=0.05)
f.text(0.08, 0.5, 'Predictions', ha='center', va='center', fontsize=20, rotation='vertical')
plt.suptitle("One big title", fontsize=18)
for ax, col in zip(axes [0], cols):
ax.set_title(col, size='large')
for ax, row in zip(axes[:, 0], rows):
ax.set_ylabel(row, size='large')