【问题标题】:Visualise most frequent words from a dataset of text in Python在 Python 中可视化文本数据集中最常见的单词
【发布时间】:2019-05-18 21:25:07
【问题描述】:

我有一个包含年份和文本(演讲稿)的 csv。

我已将其加载到数据框中并完成了预处理。

然后,我有一个新的数据框,其中包含单词及其每年的频率,如下所示,

“单词”列包含原始单词。而像“1970”这样的列包含了该“词”在该特定年份的演讲中出现的频率。因此,“年份”列包含“单词”列中提到的单词的频率。

现在,我想在一个情节中可视化每年所说的前五个单词。它可以是任何类型的可视化,如散点图。一张图中的所有数据,有 2 个轴,x 轴是年份,y 轴是频率,数据点旁边或图例中的单词。

有没有办法在 python 中做到这一点?

【问题讨论】:

    标签: nlp data-visualization visualization


    【解决方案1】:

    您可以使用annotate 为点添加标签。其余的只是管道,例如

    import matplotlib.pyplot as plt
    
    RANGE=(1970, 1974)
    plt.xticks(range(*RANGE))
    plt.xlim(RANGE)
    
    def show(year, n=5):
        "Add the top-n words for a year to the current plot"
        top5 = df.nlargest(n, columns=str(year))
        plt.scatter([year]*n, top5[str(year)])
        for _,row in top5.iterrows():
            plt.annotate(row['word'], (year, row[str(year)]))
    
    for year in range(*RANGE):
        show(year)
    

    【讨论】:

    • 非常感谢。这很有意义。但是我收到了这一行的一个关键错误: top5 = freqs_df.nlargest(n, columns=str(year)) 。键错误:'1970'
    • 非常感谢您的解决方案。关键错误是由于 str(year) 因为 year 是整数。我刚刚删除了 str() 并且一切正常。
    • 是否可以将文本注释更改为悬停文本?
    猜你喜欢
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2016-04-30
    • 2020-12-30
    • 2021-09-28
    相关资源
    最近更新 更多