使用Pandas、plt分析绘制年自杀人数直方图
1985-2016自杀情况.zip
import pandas as pd
from matplotlib import pyplot as plt
pd.set_option('display.max_colwidth',1000)
file_path = '/home/dwh/Downloads/master.csv'
d1 = pd.read_csv(file_path)
all_country = list(set(d1['country'].tolist()))
all_years = list(set(d1['year'].tolist()))
year_num_data = {'years':[], 'die_nums':[]}
for one_year in all_years:
die_num = len(list(d1['year'][d1['year'] == one_year].tolist()))
year_num_data['years'].append(one_year)
year_num_data['die_nums'].append(die_num)
year_num_data_frame = pd.DataFrame(year_num_data)
print(year_num_data_frame)
years_list = list(year_num_data_frame['years'].values)
die_num_list = list(year_num_data_frame['die_nums'].values)
plt.figure(figsize=(20,8),dpi=80)
# plt.plot(years_list, die_num_list) # 绘制点
plt.bar(years_list, die_num_list,width=0.4,color='orange')
plt.xticks(years_list) # 设置x坐标轴
plt.yticks(range(0, max(die_num_list)+1,100)) # 设置y坐标轴
plt.show()