自存
帕累托图
df=pd.read_excel('F:/Python/Python数据分析与业务应用实战/数据文件/b站订单/2月9日订单汇总.xlsx')
df=pd.DataFrame(df)
# 帕累托分析图
fig, ax1 = plt.subplots()
# 按数值大小排序
sorted_values, sorted_categories =zip(*sorted(zip(df_grouped['下单量'], df_grouped['下单时间']), reverse=True))
# 计算累计百分比
total =sum(sorted_values)
cumulative_percentages = [sum(sorted_values[:i +1]) / total *100 for iin range(len(sorted_values))]
plt.rcParams['font.sans-serif'] = ['SimHei']
ax1.bar(t, sorted_values, color='b')
for iin range(len(df_grouped['下单时间'])):
plt.text(i, sorted_categories[i] +1, str(sorted_categories[i]), ha='center', va='bottom')
ax1.set_ylabel('订单量')
ax2 = ax1.twinx()
ax2.plot(t, cumulative_percentages, 'r-o')
ax2.set_ylim([0, 100])
ax2.set_ylabel('累计百分比(%)')
plt.xticks(rotation=45)# 设置x轴标签旋转角度
ax1.legend(['订单量'], loc='upper left')
ax2.legend(['累计百分比'], loc='upper right')
plt.show()