matplotlib绘制饼图

参数说明

matplotlib.pyplot.pie(
    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)

x : 类数组 扇形大小

explode : 类数组 每个扇形半径百分比,可选

labels : 列表,每个扇形的说明,可选

colors : 类数组, 扇形颜色,可选

autopct : None,字符串,函数,百分比标签,可选,pct其实就是percent的缩写

pctdistance : float, 百分比标签的半径,可选, 默认: 0.6

shadow : bool, 是否有阴影,可选,默认: False

labeldistance : float, 标签半径,可选, 默认: 1.1

startangle : float, 开始角度,可选, 默认: None,从x轴开始

radius : float, 半径,可选, default: None(1)

counterclock : bool, 百分比方向,逆时针, 默认: True

wedgeprops : dict, 扇形属性,可选, default: None

textprops : dict, 文字属性,可选, default: None

center : pie的圆心, 可选, default: (0, 0)

frame : bool, 是否画坐标轴,可选, default: False

rotatelabels : bool, 标签是否滚动,可选, default: False

注意:上面的类数组就是指可以是列表也可以是元祖

返回值

patches : matplotlib.patches.Wedge列表(扇形实例)

texts : label matplotlib.text.Text列表(标签实例)

autotexts : label matplotlib.text.Text列表(百分比标签实例)

可以通过返回值的patches, texts, autotexts来设置扇形,标签,百分比标签的大小颜色等属性。

基础实例

#-*-coding:utf-8-*-
import matplotlib.pyplot as plt
import matplotlib

#print matplotlib.matplotlib_fname()

#用来字体集为黑体
plt.rcParams['font.sans-serif']=['SimHei']

#用来正常显示负号
plt.rcParams['axes.unicode_minus']=False


label = [u"90分以上",u"80到90",u"70到80",u"60到70",u"60分以下"]
fractions = [8,22,20,34.5,15.5]
explode = (0.1,0,0, 0, 0)

plt.pie(fractions,labels=label,explode=explode,autopct='%1.2f%%')
plt.title("pie")
plt.show()

图形大概像下面样子: pie1

上面基本上是使用matplotlib画饼图的相对简单的实例了label是设置每一个扇形的注释,fractions是设置每一个块的大小。explode用来设置块离圆心的位置。

有一个问题就是图形的中文显示问题,matplotlib的默认的font-family是sans-serif,而sans-serif设置的第一个字体是DejaVu Sans这个字体是不支持中文的。

font-family 可以看做是一组字体,程序会依次从这组字体中找到系统支持的字体为止

可以用:

print matplotlib.matplotlib_fname()

打印matplotlib的配置文件的位置。

通过:

plt.rcParams['font.sans-serif']=['SimHei']

是一种解决方法。可以直接修改matplotlib配置文件,看了很多网上的解决方案又是下载字体,又是拷贝字体,清除缓存的。其实没有那么麻烦,matplotlib是会自动搜索系统安装的字体的。可以从系统用户目录下的.matplotlib目录下的fontList.json中看就会发现是包含了系统字体的。

所有我们需要做的就是修改配置文件,sans-serif配置的字体的第一个设置为支持中文的就可以了。

配置文件是matplotlibrc,搜索一下font.family,font.serif,font.sans-serif

font.family         : sans-serif
font.serif          : Microsoft YaHei,SimHei
font.sans-serif     : Microsoft YaHei,SimHei

去掉前面的#注释,把font.sans-serif,font.serif设置为自己喜欢的字体组就可以了。其实font.family设置的那个设置那个就可以了。因为自己添加的一般都是系统支持的字体所以设置一个就可以了,这里我设置了2个字体,微软雅黑和黑体。

上面的是matplotlib画饼图的简单实例,下面我们来对比看一下,pie函数的各个参数的含义和对图形的影响。

pie参数对比

先上代码:

#-*-coding:utf-8-*-
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec


label = [u"90分以上",u"80到90",u"70到80",u"60到70",u"60分以下"]
fractions = [8,22,20,34.5,15.5]
explode = (0.3,0,0,0,0)

#生成一个3*3的网格
grids = GridSpec(3, 3)

#设置画布大小
plt.figure(figsize=(15, 10))

#定位到第一个网格
plt.subplot(grids[0, 0], aspect=1)

# autopct就是设置自动生成的百分比的输出格式
plt.pie(fractions, labels=label, autopct='%1.1f%%', shadow=True)
plt.title("shadow")

# 定位到第一行第二个
plt.subplot(grids[0, 1], aspect=1)

# startangle是这种第一个与x的角度
plt.pie(fractions, explode=explode, labels=label, autopct='%.0f%%',startangle=45)
plt.title("explode,startangle=45")

plt.subplot(grids[0, 2], aspect=1)
patches, texts, autotexts = plt.pie(fractions, labels=label,autopct='%.0f%%', radius=0.5)
for t in texts:
    t.set_size('smaller')
plt.title("radius=0.5,texts smaller")


plt.subplot(grids[1, 0], aspect=1)
# labeldistance影响的是标签离圆心的距离
patches, texts, autotexts = plt.pie(fractions, labels=label,autopct='%.0f%%',labeldistance=1.0)
for t in autotexts:
    t.set_size('x-small')
plt.title("labeldistance=1.0,autotexts x-smaller")

plt.subplot(grids[1, 1], aspect=1)
# pctdistance影响的是百分比离圆心的距离
patches, texts, autotexts = plt.pie(fractions, labels=label,autopct='%.0f%%',pctdistance=1.0)
for t in autotexts:
    t.set_color('y')
plt.title("pctdistance=1.0,autotexts color y")

plt.subplot(grids[1, 2], aspect=1)
plt.pie(fractions, labels=label,autopct='%.0f%%',rotatelabels=True)
plt.title("rotatelabels=True")


plt.subplot(grids[2, 0], aspect=1)
# counterclock设置的是顺时针填充还是逆时针填充
plt.pie(fractions, labels=label,autopct='%.0f%%',wedgeprops={'linewidth': 10},counterclock=False)
plt.title("wedgeprops,counterclock=False")

plt.subplot(grids[2, 1], aspect=1)
# frame设置是否画坐标轴
plt.pie(fractions, labels=label,autopct='%.0f%%',frame=True,radius=0.5)
plt.title("frame=True")


plt.subplot(grids[2, 2], aspect=1)
patches, texts, autotexts = plt.pie(fractions, labels=label,autopct='%.0f%%',center=(0.5,0.5),frame=True,radius=0.5)
plt.title("center=(0.5,0.5),frame=True,radius=0.5")

# figure = plt.gcf()
# figure.set_size_inches(18.5, 10.5)
# figure.savefig("pie.png")
# plt.savefig("pie.png")
plt.show()

图形大概是下面这个样子的:

matplotlib_pie2

转载于:https://my.oschina.net/u/2474629/blog/1787290

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值