relation_bar_chart.py 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. # -*- coding: utf-8 -*-
  2. # 绘制人物关系频数统计条形图
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. # 读取EXCEL数据
  6. df = pd.read_excel('人物关系表.xlsx')
  7. label_list = list(df['关系'].value_counts().index)
  8. num_list = df['关系'].value_counts().tolist()
  9. # Mac系统设置中文字体支持
  10. # plt.rcParams["font.family"] = 'Arial Unicode MS'
  11. # Windows系统设置中文字体支持
  12. plt.rcParams['font.sans-serif'] = ['SimHei']
  13. # 利用Matplotlib模块绘制条形图
  14. x = range(len(num_list))
  15. rects = plt.bar(x=x, height=num_list, width=0.6, color='blue', label="频数")
  16. # plt.ylim(0, 800) # y轴范围
  17. plt.ylabel("数量")
  18. plt.xticks([index + 0.1 for index in x], label_list)
  19. plt.xticks(rotation=45) # x轴的标签旋转45度
  20. plt.xlabel("人物关系")
  21. plt.title("人物关系频数统计")
  22. plt.legend()
  23. # 条形图的文字说明
  24. for rect in rects:
  25. height = rect.get_height()
  26. plt.text(rect.get_x() + rect.get_width() / 2, height+1, str(height), ha="center", va="bottom")
  27. # plt.show()
  28. plt.savefig('./bar_chart.png')