-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_visualizing_pieCharts.py
69 lines (39 loc) · 1.13 KB
/
stats_visualizing_pieCharts.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# coding: utf-8
# # COURSE: Master statistics and machine learning: Intuition, Math, code
# ##### COURSE URL: udemy.com/course/statsml_x/?couponCode=202006
# ## SECTION: Visualizing data
# ### VIDEO: Pie charts
# #### TEACHER: Mike X Cohen, sincxpress.com
#
# In[ ]:
# import libraries
import matplotlib.pyplot as plt
import numpy as np
# In[ ]:
## create data for the plot
nbins = 5
totalN = 100
rawdata = np.ceil(np.logspace(np.log10(1/2),np.log10(nbins-.01),totalN))
# prepare data for pie chart
uniquenums = np.unique(rawdata)
data4pie = np.zeros(len(uniquenums))
for i in range(len(uniquenums)):
data4pie[i] = sum(rawdata==uniquenums[i])
# In[ ]:
# show the pie chart
plt.pie(data4pie,labels=100*data4pie/sum(data4pie))
plt.show()
# In[ ]:
# another option
plt.pie(data4pie,labels=['zero','one','two','three','four'],explode=[0,.1,0,.15,0])
plt.show()
# In[ ]:
## for continuous data
# generate log-normal distribution
data = np.exp( np.random.randn(1000)/10 )
# generate bins using histogram
histout = np.histogram(data,bins=6)
# and show that as a pie chart
plt.pie(histout[0])
plt.show()