-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpie_chart.py
46 lines (35 loc) · 1.12 KB
/
pie_chart.py
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
#!/usr/bin/env python3
"""
Example plotting a pie chart with Matplotlib and NumPy
"""
import matplotlib.pyplot as plt
import numpy as np
# Number of random numbers to generate
SAMPLE_SIZE = 100
# Size of the plotted figure
FIGURE_SIZE = (10, 7)
# Callable used by autopct when plotting.
# The percentage argument is reverted to get the count.
def autopct(size):
def callable(percentage):
print(percentage)
value = int(round(size * percentage / 100.0))
return '{v:d} ({p:.1f}%)'.format(v = value, p = percentage)
return callable
if __name__ == "__main__":
# Random numbers in the range [1, 3]
data = np.random.randint(low=1, high=4, size=SAMPLE_SIZE)
# Map to a dictionary by counting
dict = {}
for number in data:
dict[number] = dict.get(number, 0) + 1
# Sort by key
sortedDict = sorted(dict.items())
# Map to key only
labels = [x[0] for x in sortedDict]
# Map to value only
counts = [x[1] for x in sortedDict]
# Draw chart
figure = plt.figure(figsize=FIGURE_SIZE)
plt.pie(counts, labels=labels, autopct=autopct(SAMPLE_SIZE))
plt.show()