利用利用Python+matplotlib对泰坦尼克号进行数据分析对泰坦尼克号进行数据分析
主要分析有主要分析有数据接:https://pan.baidu.com/s/1jn88GiOr7uWA8BDQocFXxg 密码: s0e0不同舱位等级中幸存者和遇难者的乘客
比例不同性别的幸存比例幸存和遇难旅客的票价分布幸存和遇难乘客的年龄分布不同上船港口的乘客仓位等级分布幸存和遇难
乘客堂兄弟姐妹的数量分布幸存和遇难旅客父母子女的数量分布单独乘船与否和幸存之间有没有联系是否成年男性和幸存之间
有没有联系
数据接数据接:https://pan.baidu.com/s/1jn88GiOr7uWA8BDQocFXxg 密码密码: s0e0
首先做准备(导入库,读入数据集)
import matplotlib.pyplot as plt
import seaborn as sns
# data = pd.read_csv('titanic.csv')
# print(data.head())
data = sns.load_dataset("titanic", data_home="titanic")
不同舱位等级中幸存者和遇难者的乘客比例不同舱位等级中幸存者和遇难者的乘客比例
classes = [] survived_s = [[], []] for pclass, items in data.groupby(by=['class']):
classes.append(pclass)
count0 = items[items['survived'] == 0]['survived'].count()
count1 = items[items['survived'] == 1]['survived'].count()
survived_s[0].append(count0)
survived_s[1].append(count1)
# 绘制图形
plt.bar(classes, survived_s[0], color='r', width=0.3)
plt.bar(classes, survived_s[1], bottom=survived_s[0], color='g', width=0.3)
# 添加文字
for i, pclass in enumerate(classes):
totals = survived_s[0][i] + survived_s[1][i] plt.text(pclass, survived_s[0][i] // 2, '%.2f%%' % ((survived_s[0][i]) / totals * 100),
ha='center')
plt.text(pclass, survived_s[0][i] + survived_s[1][i] // 2, '%.2f%%' % ((survived_s[1][i]) / totals * 100),
ha='center')
plt.xticks(classes, classes)
plt.ylim([0, 600])
plt.legend(['die', 'survive'], loc='upper right')
plt.grid(axis='y', color='gray', linestyle=':', linewidth=2)
plt.xlabel("class")
plt.ylabel("number")
plt.show()
绘图效果:
评论0