rorycostigan
rorycostigan

Reputation: 1

How to add double the amount of y values for each x value

I am graphing the population of a town but to add randomness I am adding dummy values to emulate the look of a stock price etc. . It does workshowever the years have to be doubled as I am adding two x values. 1st is the real population and the 2nd is dummy population. The only way I can think of is multiplying the the range of y values by 2 but then when I put in 10 years it is basically saying its over 20 years. This is the full code below. Sorry if I explained it poorly.

    
    from  matplotlib import __version__

    print('matplotlib version : ', __version__)

    import matplotlib.pyplot as plt
    import numpy as np
    import random

    def main():
        P = 14613
        K = 26760
        e = np.exp(1)
        R = 0.083
        T = 0
        year = 2006
    
       font1 = {'family':'serif','color':'blue','size':20}
       font2 = {'family':'serif','color':'darkred','size':15}
    
       population = []
       years = []
    
       amount_years = int(input("How many years since 2006 do you want to calcuate the population: "))
    
       for x in range(0, amount_years):
           noise = random.uniform(-0.03,0.03)
           equation = (P*K*(e**(R*T)))/((K-P)+P*(e**(R*T))) 
           dummy_pop = equation*noise
           x = equation + dummy_pop
           T += 1
           population.append(equation)
           population.append(x)
        
       for x in range(0,amount_years):
           years.append(year)
           year += 0.5
    
       xpoints = years
       ypoints = population
    
       plt.title("Portlaoise population since 2006", fontdict = font1, loc= 'left')
       plt.xlabel("Years since 2006", fontdict = font2)
       plt.ylabel("Population of Portlaoise", fontdict = font2)
    
       plt.plot(xpoints, ypoints)
       plt.show()     

     main()

Upvotes: -3

Views: 74

Answers (2)

gboffi
gboffi

Reputation: 25093

Is this what you want?

I guess that you want to plot the "analytical" population and a randomized version of it, on the same Axes. Here it is.

Please note that

  1. Numpy has a numpy.random module, import random is unnecessary.
  2. Numpy supports vectorized computations, loops are unnecessary.
  3. randomizer is uniformly distributed in the interval 0.97—1.03, is this what you want?

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(20250227)

P = 14613
K = 26760
R = 0.083
T = 0
year = 2006
ny = 12

years = np.linspace(year, year+ny, ny+1)
dt = years-year

population = P*K*np.exp(R*dt) / ((K-P)+P*(np.exp(R*dt)))
randomizer = 1+0.06*(np.random.rand(len(years))-0.5)

plt.plot(years, population,
         label=r'$\frac{PK\ \exp (R\Delta{t})}{K-P(1-\exp(R\Delta{t}))}$')
plt.plot(years, population*randomizer, label=r'ditto, randomized')

plt.legend()
plt.show()

As a bonus, the asymptotic behavior

Asymptotic behavior

Upvotes: 0

pippo1980
pippo1980

Reputation: 3094

from  matplotlib import __version__

print('matplotlib version : ', __version__)

import matplotlib.pyplot as plt
import numpy as np
import random

def main():
    P = 14613
    K = 26760
    e = np.exp(1)
    R = 0.083
    T = 0
    year = 2006
    
    font1 = {'family':'serif','color':'blue','size':20}
    font2 = {'family':'serif','color':'darkred','size':15}
    
    population = []
    years = []
    
    amount_years = int(input("How many years since 2006 do you want to calcuate the population: "))
    
    for x in range(0, amount_years):
        noise = random.uniform(-0.03,0.03)
        equation = (P*K*(e**(R*T)))/((K-P)+P*(e**(R*T))) 
        dummy_pop = equation*noise
        x = int((equation + dummy_pop)/2)
        T += 1
        #population.append(equation)
        population.append(x)
        
    for x in range(0,amount_years):
        years.append(year)
        year += 0.5
    
    xpoints = years
    ypoints = population
    
    
    
    plt.title("Portlaoise population since 2006", fontdict = font1, loc= 'left')
    plt.xlabel("Years since 2006", fontdict = font2)
    plt.ylabel("Population of Portlaoise", fontdict = font2)
    
    plt.plot(xpoints, ypoints)
    plt.show()     

main()

Output:

matplotlib version : 3.8.4

enter image description here

using this bit:

 for x in range(0, amount_years):
        noise = random.uniform(-0.03,0.03)
        equation = (P*K*(e**(R*T)))/((K-P)+P*(e**(R*T))) 
        dummy_pop = equation*noise
        x = dummy_pop
        T += 1
        #population.append(equation)
        population.append(x)

Output:

enter image description here

But I guess is uncorrect because I am not doubling Ys.

Upvotes: 0

Related Questions