- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 21387 个
- 通用积分
- 260.3014
- 学术水平
- 14 点
- 热心指数
- 25 点
- 信用等级
- 11 点
- 经验
- 754 点
- 帖子
- 4590
- 精华
- 0
- 在线时间
- 2128 小时
- 注册时间
- 2006-9-13
- 最后登录
- 2024-3-29
|
Chapter_4/practice_barchart.py- """Plot barchart of characters in text file."""
- import sys
- import os
- import operator
- from collections import Counter
- import matplotlib.pyplot as plt
- def load_file(infile):
- """Read and return text file as string of lowercase characters."""
- with open(infile) as f:
- text = f.read().lower()
- return text
- def main():
- infile = 'lost.txt'
- if not os.path.exists(infile):
- print("File {} not found. Terminating.".format(infile),
- file=sys.stderr)
- sys.exit(1)
-
- text = load_file(infile)
-
- # Make bar chart of characters in text and their frequency.
- char_freq = Counter(text)
- char_freq_sorted = sorted(char_freq.items(),
- key=operator.itemgetter(1), reverse=True)
- x, y = zip(*char_freq_sorted) # * unpacks iterable.
- fig, ax = plt.subplots()
- ax.bar(x, y)
- fig.show()
- if __name__ == '__main__':
- main()
复制代码
|
|