plot.pie画饼状图/设置颜色/设置中文显示

import pandas as pd
df1 = pd.read_csv('/home/sc/Desktop/2018-5-21/2018-7-18/contract_company_predict_03.csv')
# df2 = df1.groupby('shixin_risk_rank_actual').size()
df2 = df1.groupby('bankrupt_risk_rank').size()
print(df2)
import numpy as np
from pylab import *
mpl.rcParams['font.sans-serif'] = ['Fangsong']
mpl.rcParams['axes.unicode_minus'] = False

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import numpy as np
'''
使用 fc-list :lang=zh 命令看本地中文字体目录
'''
chinese_font_path = FontProperties(fname = '/usr/share/fonts/truetype/arphic/uming.ttc')
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
plt.figure(figsize=(6,9))

# labels = u'一般风险群', u'低风险群', u'极高风险群', u'次高风险群',u'高风险群'
labels = u'一般风险群', u'低风险群', u'极高风险群', u'次高风险群',u'高风险群'
colors = ['lightskyblue','green','red','magenta','royalblue']
# fracs = [1, 25, 27, 2,8]
fracs = [8, 1, 35, 7,12]
explode = [0, 0, 0.05, 0,0.1] # 0.1 凸出这部分,
plt.axes(aspect=1)  # set this , Figure is round, otherwise it is an ellipse
#autopct ,show percet
patches,l_text,p_text=plt.pie(x=fracs, labels=labels, explode=explode,autopct='%3.1f %%',
        shadow=True, labeldistance=1.1, startangle = 90,pctdistance = 0.6,colors=colors,

        )
for t in l_text:
    t.set_fontproperties(matplotlib.font_manager.FontProperties(fname="/usr/share/fonts/truetype/arphic/uming.ttc"))
zhfont = matplotlib.font_manager.FontProperties(fname="/usr/share/fonts/truetype/arphic/uming.ttc")

# plt.title(u"企业失信风险占比",fontproperties=zhfont,y = 1.1,fontsize = 18)
plt.title(u"企业破产风险占比",fontproperties=zhfont,y = 1.1,fontsize = 18)

'''
labeldistance,文本的位置离远点有多远,1.1指1.1倍半径的位置
autopct,圆里面的文本格式,%3.1f%%表示小数有三位,整数有一位的浮点数
shadow,饼是否有阴影
startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看
pctdistance,百分比的text离圆心的距离
patches, l_texts, p_texts,为了得到饼图的返回值,p_texts饼图内部文本的,l_texts饼图外label的文本
'''

plt.show()

猜你喜欢

转载自blog.csdn.net/sinat_26566137/article/details/81170807