Python Matplotlib顺时针饼图

ton*_*nix 4 python matplotlib pie-chart

我在玩Python及其matplotlib库,如何创建以下图表,以使第一个切片从顶部开始并向右(顺时针)移动,而不是向左(逆时针)移动:

在此处输入图片说明

码:

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()
Run Code Online (Sandbox Code Playgroud)

Lau*_* H. 7

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

patches, texts = plt.pie(sizes, colors=colors, startangle=-270)
Run Code Online (Sandbox Code Playgroud)

与:

patches, texts = plt.pie(sizes, counterclock=False, colors=colors, startangle=-270)
Run Code Online (Sandbox Code Playgroud)