1 概述
本文主要分析china,usa,king,japan,russia5个国家的gdp,时间从1800-2040,后面的数据为预测数据,不准确。
2 绘制折线图
import pandas as pd
import matplotlib.pyplot as pltdata = pd.read_csv("line_animation.csv")
x = data.timechina = data.china
usa = data.usa
king= data.king
japan= data.japan
russia= data.russiaplt.plot(x, china,'k*-.',x, usa,'g*-.',x, king,'y*-.',x, japan,'r*-.',x, russia,'b*-.')
plt.legend(["china","usa","king","japan","russia"])
plt.show()
3 计算增速
使用pct_change函数
import pandas as pd
import matplotlib.pyplot as pltdata = pd.read_csv("line_animation.csv")
# 1800 - 2020 中间还是220年
start_year = 1949-1800
end_year = 2020-1800
data = data.iloc[start_year :end_year]x = data.time
china = data.china.pct_change(1).fillna(0).apply(lambda x: round(x * 100, 2)).values
usa = data.usa.pct_change(1).fillna(0).apply(lambda x: round(x * 100, 2)).values
king = data.king.pct_change(1).fillna(0).apply(lambda x: round(x * 100, 2)).values
japan = data.japan.pct_change(1).fillna(0).apply(lambda x: round(x * 100, 2)).values
russia = data.russia.pct_change(1).fillna(0).apply(lambda x: round(x * 100, 2)).valuesplt.plot(x, china, 'k*-.',x, usa, 'g*-.',x, king, 'y*-.',x, japan, 'r*-.',x, russia, 'b*-.')
plt.legend(["china", "usa", "king", "japan", "russia"])
plt.show()
4 五国综合分析
使用柱状图进行分析均值、方差、最大值、最小值
import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_csv("line_animation.csv")
plt.rcParams['font.sans-serif']=['SimHei']china_m=data.describe()["china"]["mean"]
usa_m=data.describe()["usa"]["mean"]
king_m=data.describe()["king"]["mean"]
japan_m=data.describe()["japan"]["mean"]
russia_m=data.describe()["russia"]["mean"]
plt.subplot(221)
plt.bar(["china","usa","king","japan","russia"],[china_m,usa_m,king_m,japan_m,russia_m],width=0.5,bottom=0,align='edge',color='g',edgecolor='r',linewidth=2)
plt.text(0-0.3 ,china_m +0.05,str(round(china_m,2)))
plt.text(0+0.7,usa_m,str(round(usa_m,2)))
plt.text(0+1.7 ,king_m ,str(round(king_m,2)))
plt.text(0+2.7 ,japan_m ,str(round(japan_m,2)))
plt.text(0+3.7 ,russia_m ,str(round(russia_m,2)))
plt.title("均值")china_a=data.describe()["china"]["max"]
usa_a=data.describe()["usa"]["max"]
king_a=data.describe()["king"]["max"]
japan_a=data.describe()["japan"]["max"]
russia_a=data.describe()["russia"]["max"]
plt.subplot(222)
plt.bar(["china","usa","king","japan","russia"],[china_a,usa_a,king_a,japan_a,russia_a],width=0.5,bottom=0,align='edge',color='g',edgecolor='r',linewidth=2)
plt.title("最大值")china_i=data.describe()["china"]["min"]
usa_i=data.describe()["usa"]["min"]
king_i=data.describe()["king"]["min"]
japan_i=data.describe()["japan"]["min"]
russia_i=data.describe()["russia"]["min"]
plt.subplot(223)
plt.bar(["china","usa","king","japan","russia"],[china_i,usa_i,king_i,japan_i,russia_i],width=0.5,bottom=0,align='edge',color='g',edgecolor='r',linewidth=2)
plt.title("最小值")china_s=data.describe()["china"]["std"]
usa_s=data.describe()["usa"]["std"]
king_s=data.describe()["king"]["std"]
japan_s=data.describe()["japan"]["std"]
russia_s=data.describe()["russia"]["std"]
plt.subplot(224)
plt.bar(["china","usa","king","japan","russia"],[china_s,usa_s,king_s,japan_s,russia_s],width=0.5,bottom=0,align='edge',color='g',edgecolor='r',linewidth=2)
plt.title("方差")
plt.show()