Python Forum
Using a list as a Y value in a python graph
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using a list as a Y value in a python graph
#1

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')
Reply
#2
My bet would be you need to make x and y numpy arrays, not lists. Give it a try.
Reply
#3
Please show the actual error (in it's entirety) as it contains valuable information
Reply
#4
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')
Error:
runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Intervention Functions.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction')      Traceback (most recent call last): File "<ipython-input-9-862883395604>", line 1, in <module> runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Intervention Functions.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Intervention Functions.py", line 113, in <module> ax.plot(x, yFunction(initialPop, popGrowth), 'p-', lw=1, alpha=0.6, label='Major Graph') File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 1373, in plot for line in self._get_lines(*args, **kwargs): File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 304, in _grab_next_args for seg in self._plot_args(remaining, kwargs): File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 282, in _plot_args x, y = self._xy_from_xy(x, y) File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 223, in _xy_from_xy raise ValueError("x and y must have same first dimension") ValueError: x and y must have same first dimension
Reply
#5
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!
Reply
#6
Thanks for sharing
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo How to do a graph in Python? (and proper terminology) jpy 2 2,061 Dec-23-2020, 01:07 PM
Last Post: codeto
  Draw graph (Python) Eberle 1 3,371 May-28-2019, 05:29 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020