我正在尝试使用下面的数组绘制一个混淆矩阵。但是,当渲染热图时,列和轴标签从绘图视图中渗出,我无法弄清楚如何控制这种格式。似乎我需要一种方法来设置一些填充值。
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()
其他所有东西看起来都很不错,但是很高兴能够阅读标签!有人知道我想念什么吗?
答案 0 :(得分:3)
您的标签确实很长,所以我认为最好的选择是创建一个大图形,然后使用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()