利用numpy2000-2017各产业与行业的国民生产总值折线图和散点图
时间: 2024-06-12 20:05:26 浏览: 72
折线图:
以下是代码示例,其中数据源于国家统计局官网:
```python
import numpy as np
import matplotlib.pyplot as plt
# 读取数据
data = np.loadtxt('GDP.txt', encoding='utf-8', dtype=np.str, delimiter=',')
industries = data[0, 1:]
years = data[1:, 0].astype(np.int)
gdp = data[1:, 1:].astype(np.float)
# 绘制折线图
fig, ax = plt.subplots(figsize=(10, 6))
for i in range(gdp.shape[1]):
ax.plot(years, gdp[:, i], label=industries[i])
ax.legend(loc='best')
ax.set_xlabel('Year')
ax.set_ylabel('GDP (100 million yuan)')
ax.set_title('GDP of Industries in China (2000-2017)')
plt.show()
```
结果如下:
![折线图](https://img-blog.csdnimg.cn/20211209095855136.png)
散点图:
以下是代码示例,其中数据源于国家统计局官网:
```python
import numpy as np
import matplotlib.pyplot as plt
# 读取数据
data = np.loadtxt('GDP.txt', encoding='utf-8', dtype=np.str, delimiter=',')
industries = data[0, 1:]
years = data[1:, 0].astype(np.int)
gdp = data[1:, 1:].astype(np.float)
# 绘制散点图
fig, ax = plt.subplots(figsize=(10, 6))
for i in range(gdp.shape[1]):
ax.scatter(years, gdp[:, i], label=industries[i])
ax.legend(loc='best')
ax.set_xlabel('Year')
ax.set_ylabel('GDP (100 million yuan)')
ax.set_title('GDP of Industries in China (2000-2017)')
plt.show()
```
结果如下:
![散点图](https://img-blog.csdnimg.cn/20211209095855138.png)
阅读全文