Sickeyes
Sickeyes

Reputation: 11

How to calculate and render a growth chart in matplotlib?

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.

I am stuck on this. I know how to present growth for one year, but not sure how to do it with 10, should I repeat it 10 times, like this:

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

... and so on?

This is where I am at the moment:

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()

Upvotes: 0

Views: 1509

Answers (3)

Pedro Lobito
Pedro Lobito

Reputation: 98921

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

Upvotes: 1

Sameeresque
Sameeresque

Reputation: 2602

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()

Upvotes: 1

Vítor Cézar
Vítor Cézar

Reputation: 269

Calculate this way:

china1 = 1420062022
population = [china1 * (1.0035 ** i) for i in range(1, 11)]

Upvotes: 1

Related Questions