python绘图-饼图

使用pie绘制绘制饼图

数据来源-2023泰迪杯(b题)

代码如下:

首先导入我们需要的python库

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['axes.unicode_minus'] = False #负号显示
plt.rcParams["font.sans-serif"]=["SimHei"]#中文显示

然后读取我们的数据并对数据进行分组

#读取数据
df =pd.DataFrame(pd.read_csv(r'data_qinxi.csv',encoding='GBK'))

# 按产品大类编码、销售渠道名称分类,并计算每组的订单需求量总和
grouped_data = df.groupby(['产品大类编码', '销售渠道名称'])['订单需求量'].sum()
# 按产品细类编码、销售渠道名称分类,并计算每组的订单需求量总和
grouped_data1 = df.groupby(['产品细类编码', '销售渠道名称'])['订单需求量'].sum()

# 输出结果
print(grouped_data)
# 输出结果
print(grouped_data1)

输出结果如图:

 绘制图形:

%matplotlib auto#画布显示

first_product_category_codes = ['301', '302', '303', '304', '305', '306', '307', '308']
first_offline_sales_demand = [353886, 3009681, 2769974, 365830, 2345823, 7011384, 1204733, 1371084]
first_online_sales_demand = [545747, 926359, 250042, 34814, 826069, 2093173, 929647, 857295]
# plot the offline sales demand data
ax1 = plt.subplot2grid([2,2],(0,0))#把画布分为2×2的区域,此图在(0,0)区域
ax1.pie(first_offline_sales_demand, labels=first_product_category_codes, autopct='%1.1f%%')#autopct:小数点设置
plt.title('线下产品大类需求量')#标题设置
plt.show()
# plot the online sales demand data
ax1 = plt.subplot2grid([2,2],(0,1))
ax1.pie(first_online_sales_demand, labels=first_product_category_codes, autopct='%1.1f%%')
plt.title('线上产品大类需求量')
plt.show()


# product category codes and sales channel demand data
second_product_category_codes = ['401','402','403','404','405','406','407','408','409','410','411','412']
second_offline_sales_demand = [2307851,319809,1204733,1371084,353886,197584,6691575,3009681,365830,208750,55789,2345823]
second_online_sales_demand = [249394,177624,929647,857295,545747,72,1915549,926359,34814,348,228,826069]
ax1 = plt.subplot2grid([2,2],(1,0))
ax1.pie(second_offline_sales_demand, labels=second_product_category_codes, autopct='%1.1f%%',)
plt.title('线下产品细类类需求量')
plt.show()

# plot the online sales demand data
ax1 = plt.subplot2grid([2,2],(1,1))
ax1.pie(second_online_sales_demand, labels=second_product_category_codes, autopct='%1.1f%%')
plt.title('线上产品细类需求量')
plt.show()

 结果显示

注:如有错误,敬请指出

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会射门的18号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值