2018年大数据比赛专科组 第3部分 数据可视化第四小问

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist.axislines as axislines
from pandas import DataFrame,Series

df = pd.read_table("tiantic")

# 利用柱状图根据舱等级和性别的获救与遇难情况
# (第一、二等级为高级舱,第三等级为低级舱)
d1 = ((df["Pclass"]==3)&(df["Sex"]=='female')&(df['Survived']==0)).sum()
d2 = ((df["Pclass"]==3)&(df["Sex"]=='female')&(df['Survived']==1)).sum()
d3 = ((df["Pclass"]==3)&(df["Sex"]=='male')&(df['Survived']==0)).sum()
d4 = ((df["Pclass"]==3)&(df["Sex"]=='male')&(df['Survived']==1)).sum()
d5 = ((df["Pclass"]==1)&(df["Sex"]=='female')&(df['Survived']==0)).sum()
d6 = ((df["Pclass"]==1)&(df["Sex"]=='female')&(df['Survived']==1)).sum()
d7 = ((df["Pclass"]==1)&(df["Sex"]=='male')&(df['Survived']==0)).sum()
d8 = ((df["Pclass"]==1)&(df["Sex"]=='male')&(df['Survived']==1)).sum()
d9 = ((df["Pclass"]==2)&(df["Sex"]=='female')&(df['Survived']==0)).sum()
d10 = ((df["Pclass"]==2)&(df["Sex"]=='female')&(df['Survived']==1)).sum()
d11 = ((df["Pclass"]==2)&(df["Sex"]=='male')&(df['Survived']==0)).sum()
d12 = ((df["Pclass"]==2)&(df["Sex"]=='male')&(df['Survived']==1)).sum()
d55=d5+d9
d66=d6+d10
d77=d7+d11
d88=d8+d12
# fig表示图的大小
fig = plt.figure(figsize=(12,6))
plt.title("舱等级和性别的获救与遇难情况")
# x轴上标签的名字
list_name=['未获救',"获救"]
# 改字体
plt.rcParams['font.sans-serif']=['SimHei']
list1 = [d1,d2]
# import了一个新的函数库mpl_toolkits.axisartist.axislines为axislines
# matplotlib的子图subplot 显示多图
ax1 = axislines.Subplot(fig, 141)
fig.add_subplot(ax1)
plt.bar(range(len(list1)),list1,tick_label=list_name,fc="g")
# 每个图的条形标签
plt.legend(['女性/低级舱'], loc = "best")
# 给y轴设置刻度
ax1.set_yticks([50,100,150,200,250,300])
list2=[d3,d4]
ax2 = axislines.Subplot(fig, 142)
fig.add_subplot(ax2)
plt.bar(range(len(list2)),list2,tick_label=list_name,fc='r')
plt.legend(['男性/低级舱'], loc = 'best')
ax2.set_yticks([50,100,150,200,250,300])
list3=[d55,d66]
ax3 = axislines.Subplot(fig, 143)
fig.add_subplot(ax3)
plt.bar(range(len(list3)),list3,tick_label=list_name,fc='b')
plt.legend(['女性/高级舱'], loc = 'best')
ax3.set_yticks([50,100,150,200,250,300])
list4=[d77,d88]
ax4 = axislines.Subplot(fig, 144)
fig.add_subplot(ax4)
plt.bar(range(len(list4)),list4,tick_label=list_name,fc='y')
plt.legend(['男性/高级舱'], loc = 'best')
ax4.set_yticks([50,100,150,200,250,300])
plt.savefig("9.png")
plt.show()

上面的方法是自己写的,感觉很麻烦,很蠢,决定参考一下答案的解决方法

答案方法如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import DataFrame,Series

df = pd.read_csv("tiantic.txt",sep="\t")

# fig表示画布的大小
fig = plt.figure(figsize=(12,6))
plt.title("根据舱等级和性别的获救情况")
plt.rcParams['font.sans-serif']=['SimHei']

# 将图像分为一行四列,从左到右的第一个
ax1 = fig.add_subplot(1,4,1)
df.Survived[df.Sex  == 'female'][df.Pclass != 3].value_counts()\
    .plot(kind = 'bar',label = 'female high class',color = 'red')
ax1.set_xticklabels(['获救','未获救'],rotation=0)
ax1.set_yticks([50,100,150,200,250,300])
ax1.legend(['女性/高级舱'],loc='best')

ax2 = fig.add_subplot(1,4,2)
df.Survived[df.Sex  == 'female'][df.Pclass == 3].value_counts()\
    .plot(kind = 'bar',label = 'female low class',color = 'blue')
ax2.set_xticklabels(['获救','未获救'],rotation=0)
ax2.set_yticks([50,100,150,200,250,300])
ax2.legend(['女性/低级舱'],loc='best')

ax3 = fig.add_subplot(1,4,3)
df.Survived[df.Sex  == 'male'][df.Pclass != 3].value_counts()\
    .plot(kind = 'bar',label = 'male high class',color = 'green')
ax3.set_xticklabels(['获救','未获救'],rotation=0)
ax3.set_yticks([50,100,150,200,250,300])
ax3.legend(['男性/高级舱'],loc='best')

ax4 = fig.add_subplot(1,4,4)
df.Survived[df.Sex  == 'male'][df.Pclass == 3].value_counts()\
    .plot(kind = 'bar',label = 'male low class',color = 'pink')
ax4.set_xticklabels(['获救','未获救'],rotation=0)
ax4.set_yticks([50,100,150,200,250,300])
ax4.legend(['男性/高级舱'],loc='best')
plt.savefig("1.png")
plt.show()

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值