import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams['axes.unicode_minus']=False
data = np.load('3/国民经济核算季度数据.npz',allow_pickle=True)
print(data.files)
name = data['columns']
values = data['values']
print('\n',name,'\n',values)
plt.show()
['columns', 'values']
['序号' '时间' '国内生产总值_当季值(亿元)' '第一产业增加值_当季值(亿元)' '第二产业增加值_当季值(亿元)'
'第三产业增加值_当季值(亿元)' '农林牧渔业增加值_当季值(亿元)' '工业增加值_当季值(亿元)' '建筑业增加值_当季值(亿元)'
'批发和零售业增加值_当季值(亿元)' '交通运输、仓储和邮政业增加值_当季值(亿元)' '住宿和餐饮业增加值_当季值(亿元)'
'金融业增加值_当季值(亿元)' '房地产业增加值_当季值(亿元)' '其他行业增加值_当季值(亿元)']
[[1 '2000年第一季度' 21329.9 ... 1235.9 933.7 3586.1]
[2 '2000年第二季度' 24043.4 ... 1124.0 904.7 3464.9]
[3 '2000年第三季度' 25712.5 ... 1170.4 1070.9 3518.2]
...
[67 '2016年第三季度' 190529.5 ... 15472.5 12164.1 37964.1]
[68 '2016年第四季度' 211281.3 ... 15548.7 13214.9 39848.4]
[69 '2017年第一季度' 180682.7 ... 17213.5 12393.4 42443.1]]
绘制2000-2017年度生产总值散点图
plt.figure(figsize=(8,4))
plt.scatter(values[:,1],values[:,2],c='c',marker='*')
plt.xlabel('时间')
plt.ylabel('生产总值(亿元)')
plt.xticks(range(0,70,4),values[range(0,70,4),1],rotation=45)
plt.title('2000-2017年度生产总值散点图')
plt.show()

多个散点图绘制到同一张图里
plt.figure(figsize=(8,4))
plt.scatter(values[:,0],values[:,3],marker='o',c='red')
plt.scatter(values[:,0],values[:,4],marker='*',c='green')
plt.scatter(values[:,0],values[:,5],marker='v',c='yellow')
plt.xlabel('时间')
plt.ylabel('生产总值')
label=['第一产业','第二产业','第三产业']
plt.xticks(range(0,70,4),values[range(0,70,4),1],rotation=45)
plt.title('2000-2017各产业季度生产总值')
plt.legend(label)
plt.show()

p = plt.figure(figsize=(10,10))
ax1 = p.add_subplot(211)
plt.scatter(values[:,0],values[:,3],marker='o',c='red')
plt.scatter(values[:,0],values[:,4],marker='*',c='green')
plt.scatter(values[:,0],values[:,5],marker='v',c='yellow')
plt.ylabel('生产总值')
plt.title('2000-2017各产业季度生产总值')
label=['第一产业','第二产业','第三产业']
plt.legend(label)
ax2 = p.add_subplot(212)
plt.scatter(values[:,0],values[:,6],marker='o',c='r')
plt.scatter(values[:,0],values[:,7],marker='D',c='b')
plt.scatter(values[:,0],values[:,8],marker='v',c='y')
plt.scatter(values[:,0],values[:,9],marker='8',c='g')
plt.scatter(values[:,0],values[:,10],marker='p',c='c')
plt.scatter(values[:,0],values[:,11],marker='+',c='m')
plt.scatter(values[:,0],values[:,12],marker='s',c='k')
plt.scatter(values[:,0],values[:,13],marker='*',c='purple')
plt.scatter(values[:,0],values[:,14],marker='d',c='brown')
label=['农业','工业','建筑','批发','交通','餐饮','金融','房地产','其他']
plt.xlabel('年份')
plt.ylabel('生产总值')
plt.xticks(range(0,70,4),values[range(0,70,4),1],rotation=45)
plt.show()

颜色缩写
- b 蓝色
- g 绿色
- r 红色
- c 青色
- m 品红
- y 黄色
- k 黑色
- w 白色
折线图
data = np.load('3/国民经济核算季度数据.npz',allow_pickle=True)
print(data.files)
name = data['columns']
values = data['values']
print('\n',name,'\n',values)
['columns', 'values']
['序号' '时间' '国内生产总值_当季值(亿元)' '第一产业增加值_当季值(亿元)' '第二产业增加值_当季值(亿元)'
'第三产业增加值_当季值(亿元)' '农林牧渔业增加值_当季值(亿元)' '工业增加值_当季值(亿元)' '建筑业增加值_当季值(亿元)'
'批发和零售业增加值_当季值(亿元)' '交通运输、仓储和邮政业增加值_当季值(亿元)' '住宿和餐饮业增加值_当季值(亿元)'
'金融业增加值_当季值(亿元)' '房地产业增加值_当季值(亿元)' '其他行业增加值_当季值(亿元)']
[[1 '2000年第一季度' 21329.9 ... 1235.9 933.7 3586.1]
[2 '2000年第二季度' 24043.4 ... 1124.0 904.7 3464.9]
[3 '2000年第三季度' 25712.5 ... 1170.4 1070.9 3518.2]
...
[67 '2016年第三季度' 190529.5 ... 15472.5 12164.1 37964.1]
[68 '2016年第四季度' 211281.3 ... 15548.7 13214.9 39848.4]
[69 '2017年第一季度' 180682.7 ... 17213.5 12393.4 42443.1]]
plt.figure(figsize=(8,4))
plt.plot(values[:,0],values[:,2],c='c',ls='-.',marker='*')
plt.xlabel('时间')
plt.ylabel('生产总值(亿元)')
plt.xticks(range(0,70,4),values[range(0,70,4),1],rotation=45)
plt.title('2000-2017年度生产总值散点图')
plt.show()

plt.figure(figsize=(8,4))
plt.plot(values[:,1],values[:,3],'bs-',
values[:,1],values[:,4],'ro-.',
values[:,1],values[:,5],'gH--')
plt.xlabel('时间')
plt.ylabel('生产总值')
label=['第一产业','第二产业','第三产业']
plt.xticks(range(0,70,4),values[range(0,70,4),1],rotation=45)
plt.title('2000-2017各产业季度生产总值')
plt.legend(label)
plt.show()

p = plt.figure(figsize=(12,6))
ax1 = p.add_subplot(211)
plt.plot(values[:,1],values[:,3],'bs-',
values[:,1],values[:,4],'ro-.',
values[:,1],values[:,5],'gH--')
plt.xlabel('时间')
plt.ylabel('生产总值')
label=['第一产业','第二产业','第三产业']
plt.xticks(range(0,70,4),values[range(0,70,4),1],rotation=45)
plt.title('2000-2017各产业季度生产总值')
plt.legend(label)
ax2 = p.add_subplot(212)
plt.plot(values[:,0],values[:,6],'r--')
plt.plot(values[:,0],values[:,7],'b--')
plt.plot(values[:,0],values[:,8],'y--')
plt.plot(values[:,0],values[:,9],'g:')
plt.plot(values[:,0],values[:,10],'c-')
plt.plot(values[:,0],values[:,11],'m-')
plt.plot(values[:,0],values[:,12],'k--')
plt.plot(values[:,0],values[:,13],'r:')
plt.plot(values[:,0],values[:,14],'b-')
label=['农业','工业','建筑','批发','交通','餐饮','金融','房地产','其他']
plt.xlabel('年份')
plt.ylabel('生产总值')
plt.xticks(range(0,70,4),values[range(0,70,4),1],rotation=45)
plt.show()

分析数据内部的分布状态和分散状态。
直方图
data = np.load('3/国民经济核算季度数据.npz',allow_pickle=True)
print(data.files)
name = data['columns']
values = data['values']
print('\n',name,'\n',values)
['columns', 'values']
['序号' '时间' '国内生产总值_当季值(亿元)' '第一产业增加值_当季值(亿元)' '第二产业增加值_当季值(亿元)'
'第三产业增加值_当季值(亿元)' '农林牧渔业增加值_当季值(亿元)' '工业增加值_当季值(亿元)' '建筑业增加值_当季值(亿元)'
'批发和零售业增加值_当季值(亿元)' '交通运输、仓储和邮政业增加值_当季值(亿元)' '住宿和餐饮业增加值_当季值(亿元)'
'金融业增加值_当季值(亿元)' '房地产业增加值_当季值(亿元)' '其他行业增加值_当季值(亿元)']
[[1 '2000年第一季度' 21329.9 ... 1235.9 933.7 3586.1]
[2 '2000年第二季度' 24043.4 ... 1124.0 904.7 3464.9]
[3 '2000年第三季度' 25712.5 ... 1170.4 1070.9 3518.2]
...
[67 '2016年第三季度' 190529.5 ... 15472.5 12164.1 37964.1]
[68 '2016年第四季度' 211281.3 ... 15548.7 13214.9 39848.4]
[69 '2017年第一季度' 180682.7 ... 17213.5 12393.4 42443.1]]
label=['第一产业','第二产业','第三产业']
plt.figure(figsize=(6,5))
plt.bar(range(3),values[-1,3:6],width=0.5)
plt.xlabel('时间')
plt.ylabel('生产总值')
plt.xticks(range(3),label)
plt.title('2000-2017各产业季度生产总值')
plt.show()

饼图
plt.figure(figsize=(12,6))
label=['第一产业','第二产业','第三产业']
explode=[0.2,0.01,0.01]
plt.pie(values[-1,3:6],explode=explode,labels=label,autopct='1.1f%%')
plt.title('2017年度各产业国民生产总值')
plt.show()

箱线图
label=['第一产业','第二产业','第三产业']
gdp = (list(values[:,3]),list(values[:,4]),list(values[:,5]))
plt.figure(figsize=(6,4))
plt.boxplot(gdp,notch=False,labels=label,meanline=True)
plt.title('2000-2017各产业国民生产总值')
plt.show()

label1=['第一产业','第二产业','第三产业']
label2=['农业','工业','建筑','批发','交通','餐饮','金融','房地产','其他']
p = plt.figure(figsize=(14,8))
ax1 = p.add_subplot(221)
plt.bar(range(3),values[0,3:6],width=0.5)
plt.xlabel('产业')
plt.ylabel('生产总值')
plt.xticks(range(3),label1)
plt.title('2000年第一季度国民生产总值')
ax2 =p.add_subplot(222)
plt.bar(range(3),values[-1,3:6],width=0.5)
plt.xlabel('产业')
plt.ylabel('生产总值')
plt.xticks(range(3),label1)
plt.title('2017年第一季度国民生产总值')
ax3 =p.add_subplot(223)
plt.bar(range(9),values[0,6:],width=0.5)
plt.xlabel('行业')
plt.ylabel('生产总值')
plt.xticks(range(9),label2)
plt.title('2000年第一季度国民生产总值行业分布')
ax4 =p.add_subplot(224)
plt.bar(range(9),values[-1,6:],width=0.5)
plt.xlabel('行业')
plt.ylabel('生产总值')
plt.xticks(range(9),label2)
plt.title('2017年第一季度国民生产总值行业分布')
plt.show()

label1=['第一产业','第二产业','第三产业']
label2=['农业','工业','建筑','批发','交通','餐饮','金融','房地产','其他']
p = plt.figure(figsize=(14,8))
explode1 = [0.01,0.01,0.01]
explode2 = [0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01]
ax1 = p.add_subplot(221)
plt.pie(values[0,3:6],explode=explode1,labels=label1,autopct='%1.1f%%')
plt.title('2000年第一季度国民生产总值')
ax2 = p.add_subplot(222)
plt.pie(values[-1,3:6],explode=explode1,labels=label1,autopct='%1.1f%%')
plt.title('2017年第一季度国民生产总值')
ax3 = p.add_subplot(223)
plt.pie(values[0,6:],explode=explode2,labels=label2,autopct='%3.1f%%')
plt.title('2000年第一季度国民生产总值行业分布')
ax4 = p.add_subplot(224)
plt.pie(values[-1,6:],explode=explode2,labels=label2,autopct='%3.1f%%')
plt.title('2017年第一季度国民生产总值行业分布')
Text(0.5, 1.0, '2017年第一季度国民生产总值行业分布')
