绘制不同聚类的条形图

2024-09-30 01:33:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在学习K-means,所以现在我正在用Python编写一个程序来确定彼此相似的不同文本簇

现在我得到了两个不同聚类的结果(使用了一些虚构的词,但其他的都是一样的)

print(dfs)  = [           features     score
0  America 0.577350
1            new 0.288675
2        president 0.288675
3          Biden 0.288675
,       features     score
0       Corona 0.593578
1  COVID-19 0.296789
2     research 0.296789
3    health 0.158114]

dfs是以下类型

type(dfs) = list

以及以下各项:

type(dfs[0]) = pandas.core.frame.DataFrame

但是我如何为dfs中的每个集群创建条形图,在这些集群中,您可以看到每个单词的分数

提前谢谢


Tags: 文本程序newtype集群聚类meansscore
1条回答
网友
1楼 · 发布于 2024-09-30 01:33:36

迭代dfs列表以访问各个数据帧,然后使用df.plot.barx='featuresy='score'作为参数来绘制相对于同一数据帧的条形图。使用结果轴from plot函数将每个条的分数附加到features列中。为此,使用矩形定位点的x和条形图的height作为^{}函数的参数,从条形图轴迭代每个面片

...
...

fig, axes = plt.subplots(1, len(dfs))
for num, df in enumerate(dfs):
    ax = df.plot.bar(x='features', y='score', ax=axes[num])
    for p in ax.patches:
        ax.annotate(f'{p.get_height():.4f}', xy=(p.get_x() * 1.01, p.get_height() * 1.01))
    axes[num].tick_params(axis='x', labelrotation=30)
    axes[num].set_title(f'Dataframe #{num}')

plt.show()

cluster_barplot

相关问题 更多 >

    热门问题