我需要绘制混乱矩阵,从各个方面进行注释。我想在右侧打印与底部相同的标签时遇到问题([…类名…,“总样本数”,“精度”“mIoU]”)。此外,似乎顶部刻度与底部刻度不对齐
以下是我尝试过的:
fig, ax1 = get_new_fig('Conf matrix default', figsize)
ax = sn.heatmap(df_cm, annot=annot, annot_kws={"size": fz}, linewidths=lw, ax=ax1,
cbar=cbar, cmap=cmap, linecolor='w', fmt=fmt)
ax_new = ax.twinx().twiny()
labels = ['' for _ in range(len(ax.get_xticklabels()))]
labels[-3] = 'Total samples'
labels[-2] = 'Accuracy'
labels[-1] = 'mIoU'
ticks = [tick for tick in ax.get_xticks()]
ax_new.set_xticks(ticks)
ax_new.set_yticks(ticks)
ax_new.yaxis.set_label_position('right')
ax_new.set_xticklabels([text.get_text() for text in ax.get_xticklabels()], fontsize=10, rotation=-45) # top
ax_new.set_yticklabels(labels, fontsize=10, rotation=-25) # right
# set ticklabels
ax.set_xticklabels(labels, rotation=45, fontsize=10) # bottom
ax.set_yticklabels(ax.get_yticklabels(), rotation=25, fontsize=10) # left
我非常感谢任何帮助,因为我不知道我是否在代码中遗漏了什么
提前谢谢
问题在于新
ax
的限制。这些需要等于原始ax
的限制。尤其是原始ax
的y轴反转的事实,导致新y轴没有可见的记号标签。不同的限制也会阻止x轴记号的对齐ax_new.set_xlim(ax.get_xlim())
和ax_new.set_ylim(ax.get_ylim())
应该可以解决这个问题plt.tight_layout()
可以帮助在周围的绘图中很好地定位所有标签右y记号标签的旋转问题似乎有点难。下面的代码通过分离
twinx
和twiny
轴来解决此问题:相关问题 更多 >
编程相关推荐