大学python专业-5.Python数据可视化-美国大学各专业的性别差距

Visualizing The Gender Gap in College degrees:

读取数据:

import pandas as pd

data=pd.read_csv('./data/percent-bachelors-degrees-women-usa.csv')

print(data.columns)

#print(data[:2])

print(data.describe)

可视化:

def decorateAxes(axes):

for key,spine in axes.spines.items():

spine.set_visible(False)

axes.set_ylim(0, 100)

axes.set_yticks([0,100])

axes.axhline(y=50,c=(171/255, 171/255, 171/255),alpha=0.3)

axes.tick_params(left='off',right='off',top='off',bottom='off',labelbottom='off')

def plotCharts(axes,sub_cats,index):

axes.plot(data['Year'],data[sub_cats[index]],c=cb_dark_blue,label='Women',linewidth=3)

axes.plot(data['Year'],100-data[sub_cats[index]],c=cb_orange,label='Men',linewidth=3)

for key,spine in axes.spines.items():

spine.set_visible(False)

axes.set_title(sub_cats[index])

decorateAxes(axes)

if index==(len(sub_cats)-1):

axes.tick_params(labelbottom='on')

import matplotlib.pyplot as plt

%matplotlib inline

#Group degrees into three category.

sc_cats = ['Psychology', 'Biology', 'Math and Statistics', 'Physical Sciences', 'Computer Science', 'Engineering']

lib_arts_cats = ['Foreign Languages', 'English', 'Communications and Journalism', 'Art and Performance', 'Social Sciences and History']

other_cats = ['Health Professions', 'Public Administration', 'Education', 'Agriculture','Business', 'Architecture']

fig=plt.figure(figsize=(16,16))

cb_dark_blue = (0/255,107/255,164/255)

cb_orange = (255/255, 128/255, 14/255)

for i in range(1,17,3):

#Generate first column in line charts.Science degrees.

sc_cats_index=int(i/3)

ax=fig.add_subplot(6,3,i)

plotCharts(ax,sc_cats,sc_cats_index)

if i==1:

ax.text(2003, 85, 'Women')

ax.text(2005, 10, 'Men')

if i==16:

ax.text(2005, 87, 'Men')

ax.text(2003, 7, 'Women')

#Generate second column in line charts.Liberal arts degrees.

t=i+1

if t<15:

lib_arts_cats_index=int(t/3)

ax2=fig.add_subplot(6,3,t)

plotCharts(ax2,lib_arts_cats,lib_arts_cats_index)

if t==2:

ax2.text(2003, 85, 'Women')

ax2.text(2005, 10, 'Men')

#Generate third column in line charts.Other degrees.

r=i+2

other_cats_index=int((r-1)/3)

ax3=fig.add_subplot(6,3,r)

plotCharts(ax3,other_cats,other_cats_index)

if r==3:

ax3.text(2003, 90, 'Women')

ax3.text(2005, 5, 'Men')

if r==18:

ax3.text(2005, 62, 'Men')

ax3.text(2003, 30, 'Women')

结果如下:

MZRFFz.png

生成图片并保存:

plt.savefig("./data/genders of degrees.png")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值