繁体   English   中英

防止轴和列标签渗入matplotlib中的热图

[英]Prevent axis & column labels from bleeding off heatmap in matplotlib

我正在尝试使用下面的数组绘制一个混淆矩阵。 但是,当渲染热图时,列和轴标签从绘图视图中渗出,我无法弄清楚如何控制这种格式。 似乎我需要一种方法来设置一些填充值。

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sn

array = [
    [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0],
    [0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,1,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
    [0,0,0,0,3,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,1,0,0,2,0,0],
    [0,0,0,0,0,0,3,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
    [0,1,0,1,0,0,0,6,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,3,1,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,1,0,0,0,0,0,7,0,0,0,0,0,1,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,1,0,0,0,0,0,1,4,1,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,1,0,0,0,0],
    [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,38,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,8,0,0,0,1],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,3,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9]]

labels = ['chatbot',
    'business_passwordreset',
    'business_losthomework',
    'oos_generic',
    'frustration',
    'social_generic',
    'business_accesscode_selfstudy',
    'business_assignmentissues',
    'end_chat',
    'bye',
    'thanks',
    'business_accesscode_lost',
    'business_accesscode_redeem',
    'business_accesscode_notreceived',
    'business_accesscode_share',
    'business_accesscode_reuse',
    'business_editorial',
    'Contact_Request',
    'business_accesscode_error',
    'business_accesscode_refund',
    'hello',
    'business_accesscode_purchase',
    'business_accesscode_troubleshoot']


df_cm = pd.DataFrame(array, index=labels, columns=labels)
sn.heatmap(df_cm, annot=True, cmap='Blues')
plt.show()

渲染图: 混淆矩阵

其他所有东西看起来都很好,但是很高兴能够阅读标签! 有人知道我想念什么吗?

您的标签确实很长,所以我认为最好的选择是创建一个大图形,然后使用plt.tight_layout 如文档所述:

该模块提供了用于调整子图参数的例程,以使子图很好地适合图中

# Create a large figure so your labels aren't too crowded
plt.figure(figsize=(13,7))
df_cm = pd.DataFrame(array, index=labels, columns=labels)
sn.heatmap(df_cm, annot=True, cmap='Blues')
plt.tight_layout()

plt.show()

在此处输入图片说明

暂无
暂无

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

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