---
# System prepended metadata

title: Untitled

---

import panda as pd
import numpyas np
import matplotlib.pyplot as plt
＃# 讀取雲端硬碟中的excel檔
dfIncome=pd.read_excel("/content/drive/My Drive/__DataSet/001_Income_F.xlsx")
dfIncome

---
＃#總收入與人數
sum=dfIncome['Income'].sum()
n=dfIncome['Income'].count()
print(f'總和={sum}, 樣本數={n}')


＃#直方圖
plt.hist(dfIncome['Income'], bins=range(0, 100000, 1000))
plt.show()

----
dfLorenz=dfIncome[:]
se=dfLorenz['Income'].sort_values()
cumulativeSum=0
i=0
 宣告xx為串列變數

xx=[]
 宣告yy為串列變數
yy=[]
 手動追蹤看看
for x in se:    
    i=i+1
    cumulativeSum+=x
    xx.append(i/n)
    yy.append(cumulativeSum/sum)

plt.plot([0,1], linestyle = 'dotted')

#散佈圖
plt.scatter(x=xx,y=yy,s=0.1)
plt.axis('square')
plt.xlim(0,1)
plt.ylim(0,1)

print('羅倫茲曲線（Lorenz curve）')
plt.show()

---

R=np.trapz(x=xx,y=yy) #計算曲線下面積
A=0.5-R
G=A/(A+R)
print(G)
![](https://i.imgur.com/9X7zcJn.png)

