现有df,有字段月,门店编号,日,小时,每小时制作数量。现需要在Python中绘制每天每小时制作的数量。请写出代码
时间: 2024-03-27 15:36:44 浏览: 6
假设数据框的名称为 `df`,可以按照以下步骤绘制每天每小时制作的数量:
1. 将日期和小时合并为一个时间戳列
```python
df['timestamp'] = pd.to_datetime(df['月'] + '-' + df['日'] + ' ' + df['小时'] + ':00:00')
```
2. 按照门店编号、日期和小时分组,并计算每小时的制作数量
```python
hourly_counts = df.groupby(['门店编号', 'timestamp'])['每小时制作数量'].sum().reset_index()
```
3. 将数据转换为透视表,以便于绘制每天每小时的制作数量
```python
hourly_counts_pivot = hourly_counts.pivot(index='timestamp', columns='门店编号', values='每小时制作数量')
```
4. 使用 Matplotlib 绘图,以每小时作为 x 轴,门店编号作为线的颜色,绘制每天每小时的制作数量
```python
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(12, 8))
for store_id in hourly_counts_pivot.columns:
ax.plot(hourly_counts_pivot.index.hour, hourly_counts_pivot[store_id], label=store_id)
ax.set_xlabel('Hour')
ax.set_ylabel('Number of items produced')
ax.legend()
plt.show()
```