手头有一份性别比例的样本数据,清洗后只保留了性别信息,做了一个数据分析。
数据清洗和数据统计的代码就不贴了,贴性别比例pie图和性别比例趋势图的代码。
性别比例pie图:
def _plot_gender_stat_pie(self, fig, gender_stat, title):
"""
fig : figure obj
gender_stat : male / female stat
title : figure title
"""
def _explode(label, target='female'):
if label == target:
return 0.1
else:
return 0
labels = ['male', 'female']
expl = list(map(_explode, labels))
plt.figure(fig, figsize=(7, 7))
plt.pie(gender_stat, explode=expl, labels=labels, autopct="%5.2f%%")
plt.title(title, bbox={'facecolor': '0.8', 'pad': 8})
平均性别比例:
年份比较图:
性别比例趋势图代码:
def _plot_gender_stat_line_bar(self, gender_stats):
"""
gender_stat : male / female stats by year
"""
y = gender_stats
x = range(0, len(y)) plt.figure(figsize=(10, 6))
# line plot
plt.plot(x, y, 'r.:')
# plot text on each point
for point_x, point_y in zip(x, y):
plt.text(point_x, point_y, str('%.1f' %
point_y), horizontalalignment='center')
# bar plot
plt.bar(x, y, width=0.5, color='g')
plt.xlabel('time')
plt.ylabel('rate')
plt.title('male / female rate change', y=0.9)
plt.show()