使用 Python 中导出的数据绘制累积直方图

Plotting a cumulative histogram with exported data in Python

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

每一个都表示“vous”在文本语料库中的位置。单词 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()

但这还差得远。 我应该遵循什么步骤来完成情节?

想要的情节:

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