分别绘制2000年与2017年各产业与各行民生产总值构成分布饼图,
时间: 2024-03-03 16:50:04 浏览: 26
下面是绘制2000年与2017年各产业与各行民生产总值构成分布饼图的示例代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据
df = pd.read_excel('国民经济核算季度数据.xlsx', sheet_name='产业与行业GDP构成分布')
# 选择需要绘制的列
cols_2000 = ['第一产业GDP', '第二产业GDP', '第三产业GDP', '农林牧渔业GDP', '工业GDP', '建筑业GDP', '批发和零售业GDP', '交通运输、仓储和邮政业GDP', '住宿和餐饮业GDP', '金融业GDP', '房地产业GDP', '其他行业GDP']
cols_2017 = ['第一产业GDP.1', '第二产业GDP.1', '第三产业GDP.1', '农林牧渔业GDP.1', '工业GDP.1', '建筑业GDP.1', '批发和零售业GDP.1', '交通运输、仓储和邮政业GDP.1', '住宿和餐饮业GDP.1', '金融业GDP.1', '房地产业GDP.1', '其他行业GDP.1']
# 绘制饼图
fig, axes = plt.subplots(nrows=2, ncols=4, figsize=(16, 8))
for i, (year, cols) in enumerate(zip([2000, 2017], [cols_2000, cols_2017])):
for j, col in enumerate(cols):
ax = axes[i, j]
data = df.loc[df['时间'] == year, col].values[0]
labels = df.columns[df.columns.str.contains(col)].str.replace('.1', '').tolist()
ax.pie(data, labels=labels, autopct='%1.1f%%')
ax.set_title(f'{year}年{labels[0]}-{labels[-1]}')
plt.show()
```
在这个示例中,我们首先使用 Pandas 读取 Excel 文件中的数据,并选择需要绘制的列,分别为2000年和2017年各产业与各行民生产总值构成分布的数据。然后,我们使用 Matplotlib 的 `subplots` 函数创建一个包含8个子图的画布,并在每个子图中绘制一个饼图。我们使用 Pandas 的 `loc` 方法选择相应年份的数据,并使用 Matplotlib 的 `pie` 函数绘制饼图。最后,我们使用 Matplotlib 的 `set_title` 函数为每个子图添加标题,并使用 `show` 函数显示画布。
请注意,由于没有提供数据文件,示例代码中的数据读取代码可能需要进行修改才能适用于你的数据。