Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

""" 

Demo of a basic pie chart plus a few additional features. 

 

In addition to the basic pie chart, this demo shows a few optional features: 

 

* slice labels 

* auto-labeling the percentage 

* offsetting a slice with "explode" 

* drop-shadow 

* custom start angle 

 

Note about the custom start angle: 

 

The default ``startangle`` is 0, which would start the "Frogs" slice on the 

positive x-axis. This example sets ``startangle = 90`` such that everything is 

rotated counter-clockwise by 90 degrees, and the frog slice starts on the 

positive y-axis. 

""" 

import matplotlib.pyplot as plt 

import settings 

 

 

def category_pie_chart(categories): 

''' Pie chart, where the slices will be ordered and plotted counter-clockwise.''' 

 

# Create a list of totals for each category. 

labels = [] 

sizes = [] 

explode = [] 

other_total = 0 

for category_name in categories.keys(): 

total = 0 

for transaction in categories[category_name]: 

total += transaction.amount 

 

if total <= settings.OTHER_LIMIT: 

other_total += total 

else: 

labels.append(category_name) 

sizes.append(total) 

explode.append(0) 

 

labels.append('Other') 

sizes.append(other_total) 

explode.append(0) 

 

fig1, ax1 = plt.subplots() 

ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) 

ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. 

 

plt.show()