如何在matplotlib中计算和呈现生长图?

2024-10-03 19:32:47 发布

您现在位置:Python中文网/ 问答频道 /正文

It's said that China on day 1.7.2019 had 1 420 062 022 citizens. Number of citizens in China is increasing every year by 0,35%. Under the assumption that yearly growth of number of citizens won't change, show a graph with expected number of citizens in China in next 10 years.

我被困在这个问题上了。我知道如何表示一年的增长,但不知道如何用10表示增长,我是否应该重复10次,如下所示:

china1=1420062022
growthchina=china1*0.35/100
china2=china1+growthchina
growthchina2=china2*0.35%/100
china3=china2+growthchina

。。。等等

这就是我目前的处境:

import matplotlib.pyplot as plt
years=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
people_number=1420062022
plt.plot(years, people_number)
plt.title("Number of people in China")
plt.ylabel("(billions)")
plt.show()
plt.close()

Tags: ofinnumberthatshowitpltpeople
3条回答
import matplotlib.pyplot as plt
china=1420062022
years=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
population = [china * (1.035 ** i)/100  for i in years]
plt.plot([2020+x for x in years], population)
plt.title("Number of people in China")
plt.ylabel("(Billions)")
plt.xlabel("(Year)")
plt.show()
plt.close()

enter image description here

Demo

这样计算:

china1 = 1420062022
population = [china1 * (1.0035 ** i) for i in range(1, 11)]
import matplotlib.pyplot as plt
import numpy as np
years=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
people_number=1420062022
popln=np.zeros(len(years))
popln[0]=people_number
for i in years:
    if i!=10:
        popln[i]=popln[i-1]*1.0035

plt.plot(years, popln)
plt.title("Number of people in China")
plt.ylabel("(billions)")
plt.show()
plt.close()

相关问题 更多 >