简体   繁体   English

使用热图调整子图

[英]Adjust subplot with heatmaps

I have a Subplots with Confusion Matrixes which are presented with HeatMap . 我有一个Subplots与都带有混乱矩阵HeatMap

带有热图的子图 I would like to adjust the graph to be more readible and do things like: 我想调整图表使其更易读,并执行以下操作:

1) Add one big title above columns 'Targets' 1)在“目标”列上方添加一个大标题

2) Add one big Ylabel 'Predictions' 2)添加一个大的Ylabel“预测”

3) for each column have only one big legend, since they are showing the same thing 3)每列只有一个大图例,因为它们显示相同的内容

4 ) for each column add column names ['Train CM', 'Train Norm CM', 'Validation CM', 'Validation Norm CM'] and row names [f'Epoch {i}' for i in range(n_epoch)] . 4)为每列添加列名['Train CM', 'Train Norm CM', 'Validation CM', 'Validation Norm CM']和行名[f'Epoch {i}' for i in range(n_epoch)] I did like in here but only work for columns and not for rows, I dont know why. 我确实喜欢在这里,但仅适用于列而不适用于行,我不知道为什么。

My code: 我的代码:

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)

I am presenting a solution with empty plots because I don't have your data. 由于没有您的数据,因此我提出了一个带有空白图的解决方案。 Is this what you want: 这是你想要的吗:

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()    

在此处输入图片说明

Putting color bars : Here you put the colorbars spanning all the rows for each column. 放置颜色条 :在此处放置跨越每一列所有行的颜色条 However, here the tight_layout() isn't compatible so you will have to turn it off. 但是,这里的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')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM