Python 对数据排序后,标签文档对在绘图中出现错误
我有一个Python 对数据排序后,标签文档对在绘图中出现错误,python,python-3.x,sorting,matplotlib,numpy-ndarray,Python,Python 3.x,Sorting,Matplotlib,Numpy Ndarray,我有一个文档列表,我将其转换为稀疏矩阵 tfidf_vectorizer = TfidfVectorizer() sparse_matrix = tfidf_vectorizer.fit_transform(documents) 经过进一步的处理后,我对矩阵进行排序并绘制 问题是,排序后,我无法跟踪绘图的文档标签对。 我怎样才能解决这个问题 部分守则 path = "C:\\Users\\user\\Desktop\\texts\\dataset" text_files
文档列表
,我将其转换为稀疏矩阵
tfidf_vectorizer = TfidfVectorizer()
sparse_matrix = tfidf_vectorizer.fit_transform(documents)
经过进一步的处理后,我对矩阵进行排序并绘制
问题是,排序后,我无法跟踪绘图的文档标签对。
我怎样才能解决这个问题
部分守则
path = "C:\\Users\\user\\Desktop\\texts\\dataset"
text_files = os.listdir(path)
documents = [open(f, encoding="utf-8").read() for f in text_files if f.endswith('.txt')]
tfidf_vectorizer = TfidfVectorizer()
sparse_matrix = tfidf_vectorizer.fit_transform(documents)
labels = []
for f in text_files:
if f.endswith('.txt'):
labels.append(f)
fig, ax = plt.subplots(figsize=(20,20))
cax = ax.matshow(sim_mat_sorted, interpolation='spline16')
ax.grid(True)
plt.title('News articles similarity matrix')
plt.xticks(range(23), labels, rotation=90);
plt.yticks(range(23), labels);
fig.colorbar(cax, ticks=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])
plt.show()