下面是一个pie函数几乎所有参数的设置后的一个效果
如何查看效果
- 饼图散开 explode
- 饼图颜色 color
- 饼图数据 data
- 饼图上面的显示文本 autopct
- 饼图显示的标签 labels
- 控制饼图的线条 wedgeprops
autopack 参数有点意思
autopct enables you to display the percent value using Python string formatting. For example, if autopct='%.2f', then for each pie wedge, the format string is '%.2f' and the numerical percent value for that wedge is pct, so the wedge label is set to the string '%.2f'%pct
- autopct enables you to display the percent value using Python string formatting
小插曲英文学习 enables sb to do something 使用某物具有某种能力做某种事情
- autopct 可以让你有一种能力,什么能力呢 显示 percent 百分比的值的能力
- using Python string formatting 这个百分比的值是一个string字符串,怎么样的字符串呢?可以格式化的字符串.
- 那么 这个 autopct参数实际上就是一个展示每个饼图百分比值的作用,这个值可以根据你的需要进行格式化。也就是说这个值原始就是饼图每个部分占据的百分比
有做对参考会更容易理解上面的图
cars = ['AUDI', 'BMW', 'FORD',
'TESLA', 'JAGUAR', 'MERCEDES'
]
explode = (0.1, 0.0, 0.2, 0.3, 0.0, 0.0)
colors = ( "orange", "cyan", "brown",
"grey", "indigo",
"beige" )
data = [23, 17, 35, 29, 12, 41]
wp = { 'linewidth' : 1, 'edgecolor' : "green" }
def func(pct, allvalues):
print(pct)
absolute = int(pct / 100.*np.sum(allvalues))
val = "{:.1f}%\n({:d} g)".format(pct, absolute)
return val
fig, ax = plt.subplots(figsize =(10, 7))
wedges, texts, autotexts = ax.pie(data,
autopct = lambda pct: func(pct, data),
explode = explode,
labels = cars,
shadow = True,
colors = colors,
startangle = 90,
wedgeprops = wp,
textprops =
dict(color ="magenta"))
- def func(pct, allvalues): 函数的第一个参数实际上就是列表data中每个值占总和的percent百分比,这个由pie函数自动传入