python matplotlib画图时混淆矩阵重叠了_Python混淆矩阵matplotlib基于类别数自动调整图形大小...

当绘制大量类别的混淆矩阵时,原本清晰的图表可能会变得重叠。通过调整matplotlib函数,确保每个类别的标签都能清晰显示,避免混淆矩阵元素重叠。此问题在更新中得到了解决,通过动态调整图表大小和标签位置,使得高类别数量的混淆矩阵依然可读。
摘要由CSDN通过智能技术生成

我使用以下函数生成混淆矩阵:def plot_confusion_matrix(cm, classes, normalize=False, cmap=cm.Blues, png_output=None, show=True):

"""

This function prints and plots the confusion matrix.

Normalization can be applied by setting `normalize=True`.

"""

if normalize:

cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

title='Normalized confusion matrix'

else:

title='Confusion matrix'

f = plt.figure()

plt.imshow(cm, interpolation='nearest', cmap=cmap)

plt.title(title)

plt.colorbar()

tick_marks = np.arange(len(classes))

plt.xticks(tick_marks, classes, rotation=45)

plt.yticks(tick_marks, classes)

fmt = '.2f' if normalize else 'd'

thresh = cm.max() / 2.

for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):

plt.text(j, i, format(cm[i, j], fmt),

horizontalalignment="center",

color="white" if cm[i, j] > thresh else "black")

plt.tight_layout()

plt.ylabel('True label')

plt.xlabel('Predicted label')

if png_output is not None:

os.makedirs(png_output, exist_ok=True)

f.savefig(os.path.join(png_output,'confusion_matrix.png'), bbox_inches='tight')

if show:

plt.show()

plt.close(f)

else:

plt.close(f)

当我上几节课的时候,我会得到一张这样的图表:

但当我有大量的课程时,我会发现:

如何让我的混淆矩阵根据类的数量来调整它的大小,就像上面的boxplot解决方案一样?在

更新1

在包括了蜱虫的位置和动态飞行宽度之后

^{pr2}$

谨致问候。

克莱森·里奥斯。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值