【问题标题】:Plotting a cumulative histogram with exported data in Python在 Python 中使用导出的数据绘制累积直方图
【发布时间】:2020-05-31 18:56:37
【问题描述】:

我正在尝试绘制类似于下图的累积直方图。它显示了从单词 0 到 92,633 表示的文本语料库(x 轴)中法语代词“vous”的出现次数(y 轴)。它是使用名为 TXM 的语料库分析应用程序创建的。然而,TXM 的情节并不适合我的出版商的具体要求。我想制作自己的图,将数据导出到 python。问题是TXM导出的数据有点令人费解,想知道怎么用它来做图: 这是一个包含整数的单列 txt 文件。

每一个都表示“vous”在文本语料库中的位置。 Word 2620 是一个“vous”, 3376,另一个等。我对 Matplotlib 的尝试之一:

from matplotlib import pyplot as plt

pos = [2620,3367,3756,4522,4546,9914,9972,9979,9987,10013,10047,10087,10114,13635,13645,13646,13758,13771,13783,13796,23410,23420,28179,28265,28274,28297,28344,34579,34590,34612,40280,40449,40570,40932,40938,40969,40983,41006,41040,41069,41096,41120,41214,41474,41478,42524,42533,42534,45569,45587,45598,56450,57574,57587]
plt.bar(pos, 1)
plt.show()

但这并没有接近。 我应该按照哪些步骤来完成剧情?

想要的情节:

【问题讨论】:

    标签: python matplotlib cumulative-frequency


    【解决方案1】:

    使用 matplotlib,您可以按如下方式创建阶梯图。 where='post' 表示值在每个 x 位置发生变化,并一直保持到下一个 x 位置。 x 值是文本中的位置,前面加一个零以使图表从零出现开始。文本长度附加在末尾。 y 值是数字0, 1, 2, ...,其中重复最后一个值以完整绘制最后一步。

    from matplotlib import pyplot as plt
    from matplotlib.ticker import MultipleLocator, StrMethodFormatter
    import numpy as np
    
    pos = [2620,3367,3756,4522,4546,9914,9972,9979,9987,10013,10047,10087,10114,13635,13645,13646,13758,13771,13783,13796,23410,23420,28179,28265,28274,28297,28344,34579,34590,34612,40280,40449,40570,40932,40938,40969,40983,41006,41040,41069,41096,41120,41214,41474,41478,42524,42533,42534,45569,45587,45598,56450,57574,57587]
    text_len = 92633
    cum = np.arange(0, len(pos) + 1)
    fig, ax = plt.subplots(figsize=(12, 3))
    ax.step([0] + pos + [text_len], np.pad(cum, (0, 1), 'edge'), where='post', label=f'vous {len(pos)}')
    ax.xaxis.set_major_locator(MultipleLocator(5000)) # x-ticks every 5000
    ax.xaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}')) # use the thousands separator
    ax.yaxis.set_major_locator(MultipleLocator(5)) # have a y-tick every 5
    ax.grid(b=True, ls=':') # show a grid with dotted lines
    ax.autoscale(enable=True, axis='x', tight=True) # disable padding x-direction
    ax.set_xlabel(f'T={text_len:,d}')
    ax.set_ylabel('Occurrences')
    ax.set_title("Progression of 'vous' in TCN")
    plt.legend() # add a legend (uses the label of ax.step)
    plt.tight_layout()
    plt.show()
    

    【讨论】:

    • 这很有帮助:谢谢!我现在只是想知道,我们如何将 57587 到 92633 的数字(全为零)相加,这样 x 轴就代表整个文本语料库...
    • 我更新了代码以绘制最后一步。由于文件中没有找到92633,需要单独追加。
    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2021-06-02
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多