社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
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饼图:
例:
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的更多教程,请阅读我的全文。 在以下位置查看我的HACKERNOON个人资料和我的YouTube频道 https://youtube.com/gajeshsnaik
翻译自: https://hackernoon.com/how-to-plot-a-pie-chart-using-python-ife3ubf
python绘制饼图
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!