洛伦兹曲线(python

目录

  • 1.代码:
  • 2.效果:
  • 小结:

1.代码:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# 读入数据
file = r'123.xlsx'
sheet = 'Sheet3'
# 标题名称
title = '订货量洛伦兹曲线'
xlabel = '数量'
ylabel = '总供货'

# Define the data
df = pd.read_excel("123.xlsx", sheet_name=sheet)
var1 = df['数量'].values # 都为1即可
var2 = df['总订货'].values

#-------------------------------
# sort the data in ascending order
var1_sorted = np.sort(var1)
var2_sorted = np.sort(var2)

# calculate the cumulative sum of the sorted data
cumsum_var1 = np.cumsum(var1_sorted)
cumsum_var2 = np.cumsum(var2_sorted)

# normalize the cumulative sum by dividing by the total sum
normalized_cumsum_var1 = cumsum_var1 / np.sum(var1_sorted)
normalized_cumsum_var2 = cumsum_var2 / np.sum(var2_sorted)

# create the perfect equality line
perfect_equality_line = np.linspace(0, 1, len(var1_sorted))

#处理中文乱码
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
#-------------------------------

# plot the Lorenz curve
plt.plot(normalized_cumsum_var1, normalized_cumsum_var2, label='var1')
plt.plot([0,1], [0,1], label='Perfect equality line', linestyle='--', color='gray')
plt.xlabel('累计' + xlabel + '百分比')
plt.ylabel('累计' + ylabel + '百分比')
plt.title('洛伦兹曲线')
plt.legend()
plt.show()

修改文件名、表单名、var1、var2即可

2.效果:

洛伦兹曲线(python_第1张图片

小结:

关注我给大家分享更多有趣的知识,以下是个人公众号,提供 ||代码兼职|| ||代码问题求解||
由于本号流量还不足以发表推广,搜我的公众号即可:
在这里插入图片描述

你可能感兴趣的:(python,python,开发语言,笔记,数学建模,大数据,数据分析,经验分享)