基尼系数最开始就是衡量人群财富收入是否均衡,大家收入平平,那就是很平均,如果大家收入不平等,那基尼系数就很高。
还是给老干部们讲的言简意赅。
什么是基尼系数
我们接下来直接直观地看吧,程序说话
# -*- coding: utf-8 -*-"""
@author: 赫凯
@software: PyCharm
@file: xx.py
@time: 2023/12/15 12:57
"""import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
import matplotlibmatplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False# 基尼系数值
def gini_coef(kpi_num):cum_kpi = np.cumsum(sorted(np.append(kpi_num, 0)))sum_kpi = cum_kpi[-1]xarray = np.array(range(0, len(cum_kpi)))/np.float(len(cum_kpi)-1)yarray = cum_kpi/sum_kpiB = np.trapz(yarray, xarray)A = 0.5 - Breturn A/(A+B)# 初始化画布
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
data1 = [[10, 20, 30],[10, 20, 30, 40],[10, 20, 30, 40, 50],[10, 20, 30, 40, 50, 180],[10, 20, 30, 40, 50, 180, 200],[10, 20, 30, 40, 50, 180, 200, 1000]]def draw(num):data = data1[num]# 绘制基尼系数曲线x = range(1, len(data) + 1)x1 = [sum(x[:i]) for i in x]y1 = [sum(data[:i]) for i in x]y2 = data# 清空原有图形plt.cla()# 创建包含两个子图的图形ax1.clear()# 在第一个子图中绘制 sin 曲线ax1.plot(x, y1, 'r-')ax1.set_xticks(x, x1)# 斜线ax1.plot([x[0], x[-1]], [y1[0], y1[0]])ax1.plot([x[-1], x[-1]], [y1[0], y1[-1]])ax1.plot([x[0], x[-1]], [y1[0], y1[-1]])y_ = [(y1[-1] - y1[0]) / (x[-1] - x[0]) * (i - x[0]) + y1[0] for i in x]# 填充区域ax1.fill_between(x, y1, [y1[0] for i in x], color='g', alpha=0.3, interpolate=True)ax1.fill_between(x, y_, y1, color='pink', alpha=0.3, interpolate=True)ax1.set_xlabel('最低收入到最高收入个数累计')ax1.set_ylabel('财富累计)')ax1.set_title(f'基尼系数展示{gini_coef(y1)}')# 在第二个子图中绘制 cos 曲线ax2.bar(x, y2)ax2.set_xlabel('个人')ax2.set_ylabel('财富值')ax2.set_title('每个人的财富总览')# 创建动画对象
ani = FuncAnimation(plt.gcf(), draw, frames=len(data1), interval=500, blit=False)ani.save('xx.gif')
# 显示动画
plt.show()