python Matplotlib 系列教程(六)——绘制饼图

本章节讨论的是,饼图的绘制

饼图,常用来显示各个部分在整体里面所占有的比例。

我们选用这张图片的数据:

这里写图片描述

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)  

beijing = [17,17,23,43]
shanghai = ['19%','4%','23%','54%']
guangzhou = ['53%','25%','13%','9%']
shenzhen = ['41%','22%','20%','17%']

label = ['2-3 years','3-4 years','4-5 years','5+ years']
color = ['red','green','yellow','purple']

indic = []

#我们将数据最大的突出显示
for value in beijing:
    if value == max(beijing):
        indic.append(0.1)
    else:
        indic.append(0)

plt.pie(
    beijing,
    labels=label,
    colors=color,
    startangle=90,
    shadow=True,
    explode=tuple(indic),#tuple方法用于将列表转化为元组
    autopct='%1.1f%%'#是数字1,不是l
)


plt.title(u'饼图示例——统计北京程序员工龄', FontProperties=font)

plt.show()

这里写图片描述

附录:参数的类型

plt.stackplot(x,explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, hold=None, data=None)

param type 含义
“x” array-like 饼图各比例的数值
“explode “ array-like 是否分割出饼图的哪一块 默认是None,不分割的时候为0,分割的时候传入0.1
“labels” list 各个部分的标签default: None
“colors” array-like 饼图各比例的显示颜色
“autopct” string,function 饼图各比例的数值显示的格式
“pctdistance” float 饼图各比例的显示数据距离饼图中心的距离比例,默认是0.6
“shadow “ bool 是否绘制阴影
“labeldistance “ float 饼图标签绘制的径向距离,默认是1.1
“startangle “ float 饼图开始绘制的起始角度,如果不是None, 则从x轴逆时针旋转角度的饼图开始。
“radius “ float 饼图的半径,如果半径是None,它将被设置为1。
“counterclock” bool 指定绘制顺序方向,顺时针或逆时针,默认是True即逆时针方向
“wedgeprops” dict 设置绘制的某些样式参数等 eg:wedgeprops = {‘linewidth’: 3}
“textprops” dict 要传递给文本对象的参数字典。
“center” list of float 饼图的绘制中心,默认是(0,0)
“frame” bool 如果为真,则将坐标轴与图表一起绘制 默认:False
“rotatelabels” bool 如果为真,则将每个标签旋转到相应切片的角度。默认:False

猜你喜欢

转载自blog.csdn.net/xjl271314/article/details/80314109