tonix
tonix

Reputation: 6939

Python matplotlib clockwise pie charts

I am playing a bit with Python and its matplotlib library, how can I create the following chart so that the first slice starts from the top and goes to the right (clockwise) instead of going to the left (counter clockwise):

enter image description here

Code:

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()

Upvotes: 6

Views: 7564

Answers (1)

Laurent H.
Laurent H.

Reputation: 6526

To specify fractions direction of the pie chart, you must set the counterclock parameter to True or False (value is True by default). For your need, you must replace:

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

with:

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

Upvotes: 14

Related Questions