![]() |
Using a list as a Y value in a python graph - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Using a list as a Y value in a python graph (/thread-6919.html) |
Using a list as a Y value in a python graph - Afterdarkreader - Dec-13-2017 SO I have a function that returns a list and I want to use it as the Y value of a function. I'm currently getting an 'x and y must have the same first value' error. Anyone see the problem in my code? def y(initialPop, popGrowth): y=initialPop yList = [] x=0 while x < 400: yList.append(y) y = initialPop * np.exp(popGrowth * (x/4)) - (newsDiscussion(1, 20, x, 0.0015, 0.003, 0.55)*y)-(Tax2(156, 160, x, 0.05775, 0.1155, 0.55)*y)-(Tax1(80, 84, x, 0.77, 0.154, 0.55)*y)-(runVeganuaryMain(x)*y) x = x + 1 return yList[1:400] x= np.linspace(0, 400, num=400000) ax.set_title('cumulative decrease in consumption') ax.set_xlabel('$Quarter$') ax.set_ylabel('$Percent of the Population$') ax.plot(x, y(initialPop, popGrowth), 'p-', lw=1, alpha=0.6, label='Major Graph') RE: Using a list as a Y value in a python graph - j.crater - Dec-13-2017 My bet would be you need to make x and y numpy arrays, not lists. Give it a try. RE: Using a list as a Y value in a python graph - Larz60+ - Dec-13-2017 Please show the actual error (in it's entirety) as it contains valuable information RE: Using a list as a Y value in a python graph - Afterdarkreader - Dec-14-2017 Here's where I am now and here's the error information: def yFunction(initialPop, popGrowth): y=initialPop yList = np.array([]) x=0 while x < 400: np.append(yList, y) y = (y * np.exp(popGrowth * (x/4))) - (newsDiscussion(1, 20, x, 0.0015, 0.003, 0.55)*y)-(Tax2(156, 160, x, 0.05775, 0.1155, 0.55)*y)-(Tax1(80, 84, x, 0.77, 0.154, 0.55)*y)-(runVeganuaryMain(x)*y) x = x + 1 return yList #def xFunction(): # x = 0 # xList = np.array([]) # while x < 400: # np.append(xList, x) # x = x + 0.01 # return xList x= np.linspace(0, 400, num=400000) ax.set_title('cumulative decrease in consumption') ax.set_xlabel('$Quarter$') ax.set_ylabel('$Percent of the Population$') ax.plot(x, yFunction(initialPop, popGrowth), 'p-', lw=1, alpha=0.6, label='Major Graph')
RE: Using a list as a Y value in a python graph - Afterdarkreader - Dec-14-2017 I figured out the problem. X and Y did not have the same number of values. I adjusted my timesteps so they were the same in both and I got the program to run! RE: Using a list as a Y value in a python graph - Larz60+ - Dec-14-2017 Thanks for sharing |