最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to add double the amount of y values for each x value - Stack Overflow

programmeradmin3浏览0评论

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

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()
Share Improve this question edited Feb 14 at 22:15 pippo1980 3,0963 gold badges17 silver badges39 bronze badges asked Feb 14 at 19:07 rorycostiganrorycostigan 1 2
  • xpoints and ypoints should have the same number of elements. I don't understand what you want to achieve and why you append two values to population in each loop iteration. – jabaa Commented Feb 14 at 21:48
  • File "/lib/python3.12/site-packages/matplotlib/axes/_base.py", line 499, in _plot_args raise ValueError(f"x and y must have same first dimension, but " ValueError: x and y must have same first dimension, but have shapes (10,) and (20,) – pippo1980 Commented Feb 14 at 22:25
Add a comment  | 

1 Answer 1

Reset to default 0
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

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:

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

发布评论

评论列表(0)

  1. 暂无评论