1.分组+条形图

#所有的数据以及代码已经放到“我的资源”里面了

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv("sales4.csv",encoding = "GBK")

Step1:分组

df_group = df.groupby("产品")['第1季度','第2季度','第3季度','第4季度'].agg(np.sum)在这里插入图片描述

step2:画条形图

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

plt.rcParams['figure.figsize'] = (19, 9) 
labels = list(df_group.index)
quarter_1 = list(df_group['第1季度'].values)
quarter_2 = list(df_group['第2季度'].values)
quarter_3 = list(df_group['第3季度'].values)
quarter_4 = list(df_group['第4季度'].values)

women_means = [25, 32, 34, 20, 25]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x, quarter_1, width, label='quarter_1')
rects2 = ax.bar(x, quarter_2, width, label='quarter_2')
rects3 = ax.bar(x, quarter_3, width, label='quarter_3')
rects4 = ax.bar(x, quarter_4, width, label='quarter_4')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('金额')
# ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

# ax.bar_label(rects1, padding=3)
# ax.bar_label(rects2, padding=3)

fig.tight_layout()
plt.xticks(rotation=90) # 旋转90度

plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值