python matplotlib animation制作世界人口变动动画
主要利用animation 做了个世界人口动画,数据从网上下载的。做成json格式的。读入pandas。没有放数据, 只是弄了4个国家的。图片的很多细节也没有设置。
代码如下:
# -*- coding: UTF-8 -*-import pandas as pdimport numpy as npfrom matplotlib import pyplot as pltfrom matplotlib.animation import FuncAnimationplt.style.use("ggplot")plt.rcParams['font.sans-serif'] = ['SimHei']plt.rcParams['axes.unicode_minus'] = Falsedf = pd.read_json('{"year":{"59":1959,"58":1960,"57":1961,"56":1962,"55":1963,"54":1964,"53":1965,"52":1966,"51":1967,"50":1968,"49":1969,"48":1970,"47":1971,"46":1972,"45":1973,"44":1974,"43":1975,"42":1976,"41":1977,"40":1978,"39":1979,"38":1980,"37":1981,"36":1982,"35":1983,"34":1984,"33":1985,"32":1986,"31":1987,"30":1988,"29":1989,"28":1990,"27":1991,"26":1992,"25":1993,"24":1994,"23":1995,"22":1996,"21":1997,"20":1998,"19":1999,"18":2000,"17":2001,"16":2002,"15":,"14":,"13":,"12":,"11":,"10":,"9":,"8":,"7":,"6":,"5":,"4":,"3":,"2":,"1":,"0":},"india":{"59":4.41,"58":4.51,"57":4.6,"56":4.69,"55":4.79,"54":4.89,"53":4.99,"52":5.1,"51":5.2,"50":5.32,"49":5.43,"48":5.55,"47":5.68,"46":5.81,"45":5.95,"44":6.09,"43":6.23,"42":6.38,"41":6.52,"40":6.67,"39":6.83,"38":6.99,"37":7.15,"36":7.32,"35":7.49,"34":7.67,"33":7.84,"32":8.02,"31":8.2,"30":8.37,"29":8.55,"28":8.73,"27":8.91,"26":9.09,"25":9.27,"24":9.46,"23":9.64,"22":9.82,"21":10.01,"20":10.19,"19":10.38,"18":10.57,"17":10.75,"16":10.93,"15":11.12,"14":11.3,"13":11.48,"12":11.65,"11":11.83,"10":12.01,"9":12.18,"8":12.34,"7":12.5,"6":12.66,"5":12.81,"4":12.96,"3":13.1,"2":13.25,"1":13.39,"0":13.53},"china":{"59":6.55,"58":6.67,"57":6.6,"56":6.66,"55":6.82,"54":6.98,"53":7.15,"52":7.35,"51":7.55,"50":7.75,"49":7.96,"48":8.18,"47":8.41,"46":8.62,"45":8.82,"44":9.0,"43":9.16,"42":9.31,"41":9.43,"40":9.56,"39":9.69,"38":9.81,"37":9.94,"36":10.09,"35":10.23,"34":10.37,"33":10.51,"32":10.67,"31":10.84,"30":11.02,"29":11.19,"28":11.35,"27":11.51,"26":11.65,"25":11.78,"24":11.92,"23":12.05,"22":12.18,"21":12.3,"20":12.42,"19":12.53,"18":12.63,"17":12.72,"16":12.8,"15":12.88,"14":12.96,"13":13.04,"12":13.11,"11":13.18,"10":13.25,"9":13.31,"8":13.38,"7":13.44,"6":13.51,"5":13.57,"4":13.64,"3":13.71,"2":13.79,"1":13.86,"0":13.93},"japan":{"59":0.92,"58":0.93,"57":0.95,"56":0.96,"55":0.97,"54":0.98,"53":0.99,"52":1.0,"51":1.01,"50":1.01,"49":1.03,"48":1.04,"47":1.06,"46":1.07,"45":1.08,"44":1.1,"43":1.12,"42":1.13,"41":1.14,"40":1.15,"39":1.16,"38":1.17,"37":1.18,"36":1.18,"35":1.19,"34":1.2,"33":1.21,"32":1.21,"31":1.22,"30":1.23,"29":1.23,"28":1.24,"27":1.24,"26":1.24,"25":1.25,"24":1.25,"23":1.25,"22":1.26,"21":1.26,"20":1.26,"19":1.27,"18":1.27,"17":1.27,"16":1.27,"15":1.28,"14":1.28,"13":1.28,"12":1.28,"11":1.28,"10":1.28,"9":1.28,"8":1.28,"7":1.28,"6":1.28,"5":1.27,"4":1.27,"3":1.27,"2":1.27,"1":1.27,"0":1.27},"usa":{"59":1.78,"58":1.81,"57":1.84,"56":1.87,"55":1.89,"54":1.92,"53":1.94,"52":1.97,"51":1.99,"50":2.01,"49":2.03,"48":2.05,"47":2.08,"46":2.1,"45":2.12,"44":2.14,"43":2.16,"42":2.18,"41":2.2,"40":2.23,"39":2.25,"38":2.27,"37":2.29,"36":2.32,"35":2.34,"34":2.36,"33":2.38,"32":2.4,"31":2.42,"30":2.44,"29":2.47,"28":2.5,"27":2.53,"26":2.57,"25":2.6,"24":2.63,"23":2.66,"22":2.69,"21":2.73,"20":2.76,"19":2.79,"18":2.82,"17":2.85,"16":2.88,"15":2.9,"14":2.93,"13":2.96,"12":2.98,"11":3.01,"10":3.04,"9":3.07,"8":3.09,"7":3.12,"6":3.14,"5":3.16,"4":3.18,"3":3.21,"2":3.23,"1":3.25,"0":3.27}}')population = df.iloc[:, 1:5]country = population.keys()country = list(country)year = 3977 - df.iloc[:,0]year.sort_values(ascending=False)fig, ax = plt.subplots()y_pos = np.arange(len(country))titleList = year.sort_values(ascending=False)ax.set_xlim(0, 16)ax.set_yticks(y_pos)ax.set_yticklabels(country)ax.invert_yaxis()ax.set_xlabel('population')def animate(i):if i < 60:ax.clear()ax.set_yticks(y_pos)ax.set_yticklabels(country)ax.set_xlim(0, 16)title = ax.text(7, 3, str(titleList[i]), fontsize = 18)###关键是这一步pop = df.iloc[i, 1:5]rects = ax.barh(y_pos, pop, color = ['maroon', 'olive', 'teal', 'purple'])return rectsani = FuncAnimation(fig, animate)ani.save('population.gif',writer='pillow')
最后做成gif如图:
主要代码参考了stackoverflow里的。
/questions/56063390/how-do-i-update-the-title-and-data-of-a-matplotlib-bar-chart-animation/56065311?r=SearchResults#56065311
如果觉得《python matplotlib animation制作世界人口变动动画》对你有帮助,请点赞、收藏,并留下你的观点哦!