matplotlib.pyplot.pie
函数功能: Plot a pie chart.
绘制饼图
Make a pie chart of array x. The fractional area of each wedge is given by x/sum(x). If sum(x) < 1, then the values of x give the fractional area directly and the array will not be normalized. The resulting pie will have an empty wedge of size 1 - sum(x).
The wedges are plotted counterclockwise, by default starting from the x-axis.
绘制数组x的饼图,每个楔形的局部面积通过
x
/
s
u
m
(
x
)
x/sum(x)
x/sum(x)计算得到。如果
s
u
m
(
x
)
sum(x)
sum(x)小于1,x的值直接反应每部分楔形的面积,这时数组不会被标准化。产生的饼图会有大小为
1
−
s
u
m
(
x
)
1-sum(x)
1−sum(x)的空楔形部分。
默认情况下楔形是从从x轴开始逆时针绘制的。
函数语法: pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=0, radius=1, counterclock=True, wedgeprops=None, textprops=None, center=0, 0, frame=False, rotatelabels=False, *, normalize=None, data=None)
函数参数:
x: 1D array-like。The wedge sizes
x: 一维数据。决定楔形面积大小
explode: array-like, default: None。If not None, is a len(x) array which specifies the fraction of the radius with which to offset each wedge.
裂开:数组,默认值为空,默认情况下,各部分均不远离圆心。若非空,则为与x长度相同的数组,参数 e x p l o d e explode explode指定每个楔形距离圆心的距离是半径的多少比例
当参数
e
x
p
l
o
d
e
=
0.5
explode=0.5
explode=0.5,即每块区域都离圆心0.5倍的半径距离
当参数
e
x
p
l
o
d
e
=
0.5
explode=0.5
explode=0.5,即每块区域都离圆心0.1倍的半径距离
当参数
e
x
p
l
o
d
e
explode
explode,取值各不同,则即每块区域离圆心不同的距离
labels: list, default: None。A sequence of strings providing the labels for each wedge
标签:列表,默认值为None。是一系列字符串,提供每块楔形的标签
colors: array-like, default: None。A sequence of colors through which the pie chart will cycle. If None, will use the colors in the currently active cycle.
颜色:数组,默认为None。饼图将循环显示的一系列颜色。若为空,将使用当前活动周期中的颜色。
实践表明,颜色的个数不需要与x的长度一致,颜色会循环展示。
一种颜色展示
两种颜色展示
三种颜色展示
四种颜色展示
autopct: None or str or callable, default: None
If not None, is a string or function used to label the wedges with their numeric value. The label will be placed inside the wedge. If it is a format string, the label will be fmt % pct. If it is a function, it will be called.
数据百分比标注格式:取值为None,字符串,可调用对象,默认值为None。若非空,是用于标记楔形数值的字符串或者函数。标签会放在楔形内部。如果标签为格式字符串,其格式为
f
m
t
fmt%
fmt,若是一个函数,将会被调用。
补充:
%f格式:
**%f:**不指定宽度,整数部分全部输出,并输出6个小数位。
%m.nf:最小输出宽度为m个字符,数值精度为n。若数字宽度小于m,则在整数前面补空格;若数字宽度大于m,则全部输出
f = 5.6
g=23566.652
# 默认情况,整数部分全部输出,小数部分6位
print('%f'%f)
# 指定最小输出m与数字精度n
print('%3.2f'%f)
print('%5.2f'%f)
print('%7.2f'%f)
# 数据位数超出m,完整输出
print('%5.2f'%g)
明显可以看出当最小宽度m变大时,数字在饼图中因为在前面补空格而向后退。
pctdistance: float, default: 0.6。 The ratio between the center of each pie slice and the start of the text generated by autopct. Ignored if autopct is None.
浮点型,默认0.6。反应每块扇形的中心(每个扇形的圆心位置)与参数
a
u
t
o
p
c
t
autopct
autopct产生的数值之间的距离与半径的比例。若未设置参数
a
u
t
o
p
c
t
autopct
autopct则该参数也不生效。
当参数
p
c
t
d
i
s
t
a
n
c
e
=
0
pctdistance=0
pctdistance=0,参数
a
u
t
o
p
c
t
autopct
autopct产生的数值在每块扇形的圆心位置
当参数
p
c
t
d
i
s
t
a
n
c
e
=
0.6
pctdistance=0.6
pctdistance=0.6,参数
a
u
t
o
p
c
t
autopct
autopct产生的数值距离每块扇形圆心0.6倍距离的地方
当参数
p
c
t
d
i
s
t
a
n
c
e
=
1
pctdistance=1
pctdistance=1,参数
a
u
t
o
p
c
t
autopct
autopct产生的数值恰好在每块扇形的边缘位置
shadow: bool, default: False。Draw a shadow beneath the pie
阴影:布尔型,默认值False:无阴影。绘制饼图的阴影
当参数
s
h
a
d
o
w
=
T
r
u
e
shadow=True
shadow=True,结果展示如下:
- 尚不理解
- normalize: None or bool, default: None When True, always make a full pie by normalizing x so that sum(x) == 1. False makes a partial pie if sum(x) <= 1 and raises a ValueError for sum(x) >1.When None, defaults to True if sum(x) >= 1 and False if sum(x) <1.Please note that the previous default value of None is now deprecated, and the default will change to True in the next release. Please pass normalize=False explicitly if you want to draw a partial pie.
labeldistance: float or None, default: 1.1。 at which the pie labels are drawn. If set to None, label are not drawn, but are stored for use in legend()
标签距离:浮点型或者None,默认浮点型1.1。饼图标签的径向距离。若设置为None,标签不会被展示,但是可以展示在legend中
参数
l
a
b
e
l
d
i
s
t
a
n
c
e
=
1
labeldistance=1
labeldistance=1
参数
l
a
b
e
l
d
i
s
t
a
n
c
e
=
1.2
labeldistance=1.2
labeldistance=1.2
参数
l
a
b
e
l
d
i
s
t
a
n
c
e
=
N
o
n
e
labeldistance=None
labeldistance=None,标签不展示
参数
l
a
b
e
l
d
i
s
t
a
n
c
e
=
N
o
n
e
labeldistance=None
labeldistance=None,标签在图例中展示
若参数
l
a
b
e
l
d
i
s
t
a
n
c
e
labeldistance
labeldistance使用默认值1.1,同时有legend,则展示一次标签,一次图例。
startangle: float, default: 0 degrees。 The angle by which the start of the pie is rotated, counterclockwise from the x-axis.
起始角:浮点型,默认值是0°,起始饼旋转的角度,从x轴逆时针旋转。
radius: float, default: 1。The radius of the pie.
半径:浮点型,默认长度为1。饼的半径
参数
r
a
d
i
u
s
=
1.5
radius=1.5
radius=1.5,此图明显变大。
counterclock: bool, default: True。Specify fractions direction, clockwise or counterclockwise.
是否逆时针绘图:布尔型,默认值为True逆时针。指定绘制图形的顺时针与逆时针
参数
c
o
n
u
t
e
r
c
l
o
c
k
=
F
a
l
s
e
conuterclock=False
conuterclock=False,顺时针绘图。
wedgeprops: dict, default: None
Dict of arguments passed to the wedge objects making the pie. For example, you can pass in wedgeprops = {‘linewidth’: 3} to set the width of the wedge border lines equal to 3. For more details, look at the doc/arguments of the wedge object. By default clip_on=False.
楔形边界属性设置:字典类型,默认值None.字典参数传递给楔形对象来绘制饼图。例如:传递
′
l
i
n
e
w
i
d
t
h
′
:
3
{'linewidth': 3}
′linewidth′:3 来设置楔形边界线的宽度等于3。更多详细内容查看楔形对象的文档/参数。
textprops: dict, default: None
Dict of arguments to pass to the text objects.
文本属性:字典属性,默认值:None
传递给文本对象的参数字典
center: (float, float), default: (0, 0)
The coordinates of the center of the chart.
中心:一对浮点型坐标,默认位置:(0,0)
图标中的坐标
设置参数
c
e
n
t
e
r
=
(
6
,
6
)
center=(6,6)
center=(6,6)饼状图圆心在(-6,-6)位置,因为鼠标点的时候没有精确定位,所以显示的数值有一点偏差
参数
c
e
n
t
e
r
center
center为空,默认在(0,0)位置
frame: bool, default: False
Plot axes frame with the chart if true.
框架:布尔型,默认False:无框架
若为真,绘制图标的坐标系框架。
设置参数
f
r
a
m
e
=
T
r
u
e
frame=True
frame=True,绘制出来一个框架!这个框架的范围从(0,0)到(1,1)
rotatelabels: bool, default: False
Rotate each label to the angle of the corresponding slice if true.
旋转标签文本:布尔型,默认值False:不旋转
若为真,将标签旋转到相应角度
参数
r
o
t
a
t
e
l
a
b
e
l
s
=
30
rotatelabels=30
rotatelabels=30
官方文档:链接 https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html