0

我正在尝试绘制类似于下图的累积直方图。它显示了从单词 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()

但这并不接近。我应该遵循哪些步骤来完成情节?

想要的情节:

想要的情节

4

1 回答 1

0

使用 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()

示例图

于 2020-05-31T19:31:07.580 回答