2

我试图在图表上绘制基尼系数,显示不同变量(GDP_PPP 和 GDP_MER)的不同数据,以显示 gdp 市场汇率与作为国民总收入的 gdp 之间的不平等。我正在努力让两个变量出现在同一个图表上。

下面的代码是我为计算基尼系数函数所做的:

def GiniCoeff_plot(pop, resource):

    iuse = (np.logical_not(pop.isnull()) & np.logical_not(resource.isnull())) & np.greater(np.float64(resource),0)

    pop = pop[iuse]; pop = np.array(np.float64(pop)) 
    resource = resource[iuse]; resource = np.array(np.float64(resource)) 
    resource = resource/pop 
    resource2 = resource2[iuse]; resource = np.array(np.float64(resource2)) 
    resource2 = resource2/pop 
    
    pw = np.array([pop.tolist(), resource.tolist(), resource2.tolist()]); pw = pw.T 
    pw = pw[pw[:,1].argsort()]; (r, c) = pw.shape 

    pw2 = pw2[pw2[:,1].argsort()]; (r, c) = pw2.shape 

    fy = pw[:,0]/sum(pw[:,0]) 
    F = np.zeros(r+1); S = np.zeros(r+1); L = np.zeros(r+1) 
    F[1:r+1] = np.cumsum(fy) 
    S[1:r+1] = np.cumsum(fy*pw[:,1]) 
    S2[1:r+1] = np.cumsum(fy*pw[:,1]) 
    L = S/S[-1] 
    L2 = S2/S2[-1] 

    partarea1 = np.sum(fy.T * L[0:r])
    partarea2 = np.sum(fy.T * L[1:r+1])
    G = 1 - partarea1 - partarea2
    
    #ploting data
    plotflag=1
    if plotflag==1:
        plt.plot(F, L, '-b', label="observed")
        plt.plot(F, F, '--k', label="perfect eq.")
        plt.plot(F, L2, '-y', label='test')
        plt.xlabel("Population")
        plt.xticks(np.arange(0,1.1, step=0.1), ["{}%".format(i) for i in range(0,110,10)])
        plt.ylabel("Resources")
        plt.yticks(np.arange(0,1.1, step=0.1), ["{}%".format(i) for i in range(0,110,10)])
        plt.grid(color='k', linestyle='-', linewidth=0.1)
        plt.title("GINI: %.4f" %(G))
        plt.legend()
        plt.show() 
        
    return G

和:

    pop =  pd.DataFrame(C04["pop"]); pop = pop[pop.applymap(isnumber)]; pop=pop["pop"] 
    resource =  pd.DataFrame(C04["gdp_mer"]); resource = resource[resource.applymap(isnumber)]; resource = resource["gdp_mer"]
    resource2 =  pd.DataFrame(C04["gdp_ppp"]); resource2 = resource2[resource2.applymap(isnumber)]; resource2= resource2["gdp_ppp"]
    
    G = GiniCoeff_plot(pop, resource, resource2)
    print("G = {:.4f}".format(G))

但是我不断收到错误消息:"TypeError: GiniCoeff_plot()接受 2 个位置参数,但给出了 3 个“

有谁知道我可以如何更改它以便我可以包含这两个变量?任何帮助将不胜感激!

4

1 回答 1

0

除非我弄错了,否则您只是忘记将参数添加到函数定义中。

您目前拥有:

def GiniCoeff_plot(pop, resource):

但在内部,您同时指代resourceresource2

我认为您需要做的就是更新函数声明以接受resource2作为参数,而不是简单地在内部创建它:

def GiniCoeff_plot(pop, resource, resource2):
于 2021-04-22T21:25:17.180 回答