中培伟业IT资讯频道
您现在的位置:首页 > IT资讯 > 软件研发 > 如何使用Python绘制饼图

如何使用Python绘制饼图

2020-08-19 17:33:02 | 来源:中培企业IT培训网

这里说的饼图是软件开发领域的,饼图是圆形统计图,整个图表的区域代表100%或全部数据,饼图中显示的饼图区域代表数据部分的百分比。饼图的各个部分称为楔形,楔形的弧长决定了饼图中楔形的面积,楔形的面积确定零件相对于整体的相对量子或百分比。饼图经常用于业务演示中,因为它们可以快速总结业务活动,例如销售,运营等。饼形图还大量用于调查结果,新闻文章,资源使用图中。那么如何使用Python绘制饼图?

  使用Python Matplotlib绘制简单的饼图

可以使用pyplot模块中的pie()函数绘制饼图。以下python 代码示例使用pie()函数绘制了一个饼图。

默认情况下,pyplot的pie()功能沿逆时针方向排列饼图中的饼形或楔形。

例:

# import the pyplot library

import matplotlib.pyplot as plotter

# The slice names of a population distribution pie chart

pieLabels = 'Asia', 'Africa', 'Europe', 'North America', 'South America', 'Australia'

# Population data

populationShare = [59.69, 16, 9.94, 7.79, 5.68, 0.54]

figureObject, axesObject = plotter.subplots()

# Draw the pie chart

axesObject.pie(populationShare,

labels=pieLabels,

autopct='%1.2f',

startangle=90)

# Aspect ratio - equal means pie is a circle

axesObject.axis('equal')

plotter.show()

  在饼图中创建楔形以爆炸:

使用饼图函数的explode参数,可以使饼图的楔形从饼图的其余楔形中爆炸。

explode 是一个元组,其中每个元素都对应于饼图的楔形。

元组的长度应等于饼图中的饼图数。

除此以外 matplotlib 将引发类型的异常 ValueError。

例:

explodeTuple = (0.1, 0.0, 0.0, 0.0, 0.0, 0.0)

# Draw the pie chart

axesObject.pie(populationShare, explode=explodeTuple,

labels=pieLabels,

autopct='%1.2f',

startangle=90)

在上面的代码片段中,pie()函数与元组 一起传递 给explode参数。元组中只有第一个元素 是小数,而其他所有元素都为零,这将使饼图的第一个楔形图与其余的图看起来不同。

  使饼图的多个楔形爆炸:

上面的示例仅使饼图的楔形之一爆炸。

在以下代码段中,将一个元组提供给pie()函数的explode参数。元组的第一个和最后一个条目为非零分数,从而使饼图的第一个和最后一个楔形爆炸。

基于元组中元素的数值,楔形会爆炸到离饼图中心点相对较高或较低的距离。

负值将使楔形出现在与指定的原始位置相反的方向上。

例:

explodeTuple = (0.1, 0.0, 0.0, 0.0, 0.0, 0.8)

# Draw the pie chart

axesObject.pie(populationShare, explode=explodeTuple,

labels=pieLabels,

autopct='%1.2f',

startangle=90)

  在使用Matplotlib绘制的饼图中自定义颜色:

可以使用pie()函数的参数颜色来自定义饼图中楔形的颜色。colors参数是一个元组,其中包含要为饼图的楔形循环的颜色列表。

例:

# import the pyplot library

import matplotlib.pyplot as plotter

# Guest age group

ageGroupLabel = 'Below 5', '5-10', '10-15', '15-20', '20-30', '30-40', '40-50', '50-60', '60-80', '80-100', 'Above 100'

# Number of Guests expected in age group

guestNumbers = [5, 10, 10, 15, 10, 30, 25, 25, 20, 15, 10]

figureObject, axesObject = plotter.subplots()

explode = (0.4, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.3)

colors = ("red", "green", "orange", "cyan", "brown", "grey", "blue", "indigo", "beige", "yellow")

# Draw the pie chart

axesObject.pie(guestNumbers,

explode = explode,

colors = colors,

labels = ageGroupLabel,

autopct = '%1.2f',

startangle = 90)

# Aspect ratio

axesObject.axis('equal')

plotter.show()

在输出中,由于在达到“ 100岁以上”年龄组时所有颜色均已用尽,因此将从头开始再次进行颜色设置,即将红色分配给与该年龄段的儿童相同的“ 100岁以上”年龄组。

  自定义Matplotlib饼图:

1.可以自定义Matplotlib.pyplot绘制的饼图的几个方面。

2.startangle参数将饼图旋转指定的度数。旋转是逆时针方向,并在饼图的X轴上执行。

3.可以使用pie()函数的shadow参数提供阴影效果。传递True将使阴影出现在饼图边缘下方。默认情况下,shadow的值为False,并且饼图不会有阴影。

4.可以使用楔子属性参数进一步定制饼图的楔子。可以使用名称值对描述楔形属性的python字典作为楔形属性参数来传递。

5.通过将frame参数设置为True,将围绕饼图绘制轴框架。

6.arc()函数的Autopct参数控制百分比在楔形中的显示方式。可以指定以%开头的格式字符串,也可以指定函数。例如,%。1f将以25.0、35.2等格式显示百分比值。%.2f %%将以50.25、75.5等格式显示百分比值。

例:

import matplotlib.pyplot as plotter

# Distribution of n-grams in a paragraph - maximum wordlength 5

wedgeLabels = ("Unigram", "Bigram", "Trigram", "Four-gram", "Five-gram", "Others")

# Percentage of n-grams

ngramPercent = (5, 5, 10, 5, 10, 65)

figureObject, axesObject = plotter.subplots()

axesObject.pie(ngramPercent,

labels = wedgeLabels,

shadow = True,

frame = True,

startangle = 120,

autopct = '%.1f%%',

wedgeprops = { 'linewidth' : 3,

'edgecolor' : "orange" })

axesObject.axis('equal')

plotter.show()

以上就是关于如何使用Python绘制饼图的全部内容,想了解更多关于Python的信息,请继续关注中培伟业。