目录

  • 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_大数据

小结:

关注我给大家分享更多有趣的知识,以下是个人公众号

洛伦兹曲线(python_开发语言_02