使用Pandas、plt分析绘制年自杀人数直方图

1985-2016自杀情况.zip

  1. import pandas as pd
  2. from matplotlib import pyplot as plt
  3. pd.set_option('display.max_colwidth',1000)
  4. file_path = '/home/dwh/Downloads/master.csv'
  5. d1 = pd.read_csv(file_path)
  6. all_country = list(set(d1['country'].tolist()))
  7. all_years = list(set(d1['year'].tolist()))
  8. year_num_data = {'years':[], 'die_nums':[]}
  9. for one_year in all_years:
  10. die_num = len(list(d1['year'][d1['year'] == one_year].tolist()))
  11. year_num_data['years'].append(one_year)
  12. year_num_data['die_nums'].append(die_num)
  13. year_num_data_frame = pd.DataFrame(year_num_data)
  14. print(year_num_data_frame)
  15. years_list = list(year_num_data_frame['years'].values)
  16. die_num_list = list(year_num_data_frame['die_nums'].values)
  17. plt.figure(figsize=(20,8),dpi=80)
  18. # plt.plot(years_list, die_num_list) # 绘制点
  19. plt.bar(years_list, die_num_list,width=0.4,color='orange')
  20. plt.xticks(years_list) # 设置x坐标轴
  21. plt.yticks(range(0, max(die_num_list)+1,100)) # 设置y坐标轴
  22. plt.show()

image.png