Python Seaborn 小提琴图
#https://www.jianshu.com/p/96977b9869ac 这是个小提琴图的简书教程···
import pandas as pd
import numpy as np
import seaborn as sns
#encoding=utf-8
import matplotlib.pyplot as plt
import sys
reload (sys)
sys.setdefaultencoding('utf8')#解决编码问题
sns.set(style = 'white',context = 'notebook')#设置风格
sns.set_style('whitegrid',{'font.sans-serif':['sihei','Arial']})#处理中文字体
data = pd.read_csv('/Users/punky/Downloads/地市级党委书记数据库.csv',encoding = 'utf-8')
data.shape
data.head()
data.columns
data_gender2 = data[[u'省级政区名称',u'性别']]
data_gender2_re = data_gender2[data_gender2[u'性别'].notnull()]
pt = pd.crosstab(data_gender2_re[u'省级政区名称'],data_gender2[u'性别'])
pt[u'女性占比'] = pt[u'女']/(pt[u'男']+pt[u'男'])
pt2 = pt.sort_values(by = [u'女性占比'], ascending = False)
x = ['Percentage of Women leaders_structure']
y = pt2[u'女性占比'].tolist()
fig = plt.figure(figsize = (12,8))
sns.violinplot(data = y,orient="h")
plt.title('Percentage of Women Leaders')
plt.xlabel('Per %')
sns.despine(bottom=True)