Python matplotlib 顺时针饼图

5
我正在使用Python和其 matplotlib 库进行实验,如何创建以下图表,使第一个切片从顶部开始向右 (顺时针) 而不是向左 (逆时针):

enter image description here

代码:
import matplotlib.pyplot as plt
import re
import math

# The slices will be ordered and plotted counter-clockwise if startangle=90.
sizes = [175, 50, 25, 50]
total = sum(sizes)
print('TOTAL:')
print(total)
print('')
percentages = list(map(lambda x: str((x/(total * 1.00)) * 100) + '%', sizes))

print('PERCENTAGES:')
print(percentages)
backToFloat = list(map(lambda x: float(re.sub("%$", "", x)), percentages))
print('')

print('PERCENTAGES BACK TO FLOAT:')
print(backToFloat)
print('')

print('SUM OF PERCENTAGES')
print(str(sum(backToFloat)))
print('')
labels = percentages
colors = ['blue', 'red', 'green', 'orange']
patches, texts = plt.pie(sizes, colors=colors, startangle=-270)


plt.legend(patches, labels, loc="best")
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.tight_layout()
plt.show()
1个回答

11

为了指定饼图的分数方向,您必须将counterclock参数设置为TrueFalse(默认值为True)。根据您的需要,您必须替换:

patches, texts = plt.pie(sizes, colors=colors, startangle=-270)

使用:

patches, texts = plt.pie(sizes, counterclock=False, colors=colors, startangle=-270)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接