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

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    Im graphing the population of a town but to add randomness im adding dummy values to emulate the look of a stock price etc. it works however the years have to be doubled as im adding two x values. 1 is the real population and the next 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 its basically saying its over 20 years. this is the full code below. sorry if i poorly explained.

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

    Continue reading...

Compartilhe esta Página