Python绘制0-14岁15-59岁60岁及以上的人口年龄分布动态玫瑰饼状图
实现这个任务需要用到Python中的matplotlib库,下面是具体的代码实现:
import matplotlib.pyplot as plt
import numpy as np
# 设置年龄段和人口数量
age_groups = ['0-14岁', '15-59岁', '60岁及以上']
populations = [0.2, 0.6, 0.2]
colors = ['red', 'green', 'blue']
# 绘制初始的玫瑰饼状图
ax = plt.subplot(111, projection='polar')
ax.set_theta_direction(-1)
ax.set_theta_zero_location('N')
ax.bar(x=np.arange(len(age_groups)) * 2 * np.pi / len(age_groups),
height=populations, width=2*np.pi/len(age_groups),
color=colors, alpha=0.5)
# 绘制动态效果
for i in range(1, 21):
ax.clear()
ax.set_theta_direction(-1)
ax.set_theta_zero_location('N')
ax.bar(x=np.arange(len(age_groups)) * 2 * np.pi / len(age_groups),
height=[p * i / 20 for p in populations],
width=2*np.pi/len(age_groups), color=colors, alpha=0.5)
plt.title('人口年龄分布动态玫瑰饼状图 ({0}/{1})'.format(i, 20))
plt.pause(0.1)
plt.show()
代码中首先定义了年龄段和人口数量,并设置了各个年龄段对应的颜色。然后通过matplotlib库绘制了初始的玫瑰饼状图。
接着,通过for循环模拟动态效果,每次循环都清除原有的图形并绘制新的图形。在每个循环中,通过设置height参数来实现动态效果,即每个年龄段对应的人口数量随着循环次数的增加而逐渐增多。
最后通过plt.show()函数显示最终的图形。运行上述代码即可得到人口年龄分布动态玫瑰饼状图
原文地址: https://www.cveoy.top/t/topic/fHVn 著作权归作者所有。请勿转载和采集!