import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # 设置风格,seaborn有5种基本风格,context表示环境 sns.set(style="white", context="notebook") # 处理中文问题 sns.set_style('whitegrid', {'font.sans-serif':['simhei', 'Arial']}) data = pd.read_csv('D:\\myfiles\\study\\python\\analyse\\数据团\\城市数据团_数据分析师_体验课_课程资料\\数据资料\\地市级党委书记数据库(2000-10).csv', encoding='gbk') data_gender2 = data[['省级政区名称','性别']] data_gender2_re = data_gender2[data_gender2['性别'].notnull()] pt = pd.crosstab(data_gender2_re['省级政区名称'], data_gender2_re['性别']) pt['女性占比'] = pt['女'] / (pt['女'] + pt['男']) pt2 = pt.sort_values(by = ['女性占比'], ascending = False) fig = plt.figure(figsize=(10,4)) x = ['辽宁','山西','吉林','陕西','四川','安徽','江西','山东','福建','云南'] pt2['女性占比百分数'] = pt2['女性占比'] *100 y = pt2[:10]['女性占比百分数'].tolist() # 设定x,y值 sns.barplot(x, y, palette="BuPu_r") plt.title('女性占比百分数') plt.ylabel('Per %') # 数据可视化:柱状图 sns.despine(bottom=True) plt.show()