Posts: 23
Threads: 10
Joined: Dec 2017
I have a pretty complex calculation that calls several different functions. One of them needs to loop. I think the method I have here SHOULD be working, but it keeps giving me an 'X and Y must have the same first dimension' error. Anyone see my problem?
import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt
from math import exp
fig, ax = plt.subplots(1, 1)
initialPop = 326300000
popGrowth = 0.007
def Veganuary(StartQ_A1, EndQ_A1, CurrentQ_A1, Mean_A1, Cum_impact_A1, Peak_factor_A1):
Scale_A1=exp(Mean_A1)
return lognorm.cdf(CurrentQ_A1, Peak_factor_A1, StartQ_A1, Scale_A1)*Cum_impact_A1
def runVeganuaryMain(x):
testTime = 52
while testTime < 400:
Veganuary(testTime, testTime+1, x, 0.006, 0.012, 0.55)
testTime = testTime + 4
return
x= np.linspace(0, 216, num=86)
ax.set_title('cumulative decrease in consumption')
ax.set_xlabel('$Quarter$')
ax.set_ylabel('$Percent of the Population$')
ax.plot(x, runVeganuaryMain(x), 'p-', lw=1, alpha=0.6, label='Major Graph Error: runfile('C:/Users/after/OneDrive/Python Scripts/DistinctionProject2017/Figure out this veganuary bs.py', wdir='C:/Users/after/OneDrive/Python Scripts/DistinctionProject2017')

Traceback (most recent call last):
File "<ipython-input-90-e0be8571f5f4>", line 1, in <module>
runfile('C:/Users/after/OneDrive/Python Scripts/DistinctionProject2017/Figure out this veganuary bs.py', wdir='C:/Users/after/OneDrive/Python Scripts/DistinctionProject2017')
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/Python Scripts/DistinctionProject2017/Figure out this veganuary bs.py", line 44, in <module>
ax.plot(x, runVeganuaryMain(x), '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
Posts: 103
Threads: 15
Joined: Nov 2017
Dec-09-2017, 09:27 PM
(This post was last modified: Dec-09-2017, 09:28 PM by hshivaraj.)
def runVeganuaryMain(x):
testTime = 52
while testTime < 400:
Veganuary(testTime, testTime+1, x, 0.006, 0.012, 0.55)
testTime = testTime + 4
return This function seems to return nothing?! Shouldn't it be the y axis values?
Posts: 23
Threads: 10
Joined: Dec 2017
see but if I do this:
def runVeganuaryMain(x):
testTime = 48
while testTime < 400:
testTime = testTime + 4
return Veganuary(testTime, testTime+1, x, 0.006, 0.012, 0.55) it pulls me out of the while loop without it looping
Posts: 103
Threads: 15
Joined: Nov 2017
Well ofcourse, i dont think the control doesnt even gets there. Your problem is here
ax.plot(x, runVeganuaryMain(x), 'p-', lw=1, alpha=0.6, label='Major Graph the second argument to the function plot should be double? Right?
Now runVeganuaryMain should return a value which can be plotted. Looking at what you've posted, function runVeganuaryMain doesn't return anything. It fact it return None
Posts: 1,150
Threads: 42
Joined: Sep 2016
return statement ends the execution immediately, no matter where in function it is placed. Perhaps what you need is appending the calculated value to a list in each while iteration, and then returning that list which you feed to plot function?
Posts: 33
Threads: 3
Joined: Dec 2017
(Dec-09-2017, 09:50 PM)Afterdarkreader Wrote: see but if I do this:
def runVeganuaryMain(x):
testTime = 48
while testTime < 400:
testTime = testTime + 4
return Veganuary(testTime, testTime+1, x, 0.006, 0.012, 0.55) it pulls me out of the while loop without it looping
yes, thats because return is indented so that its inside your while loop.
you want to remove 4 spaces so that its AFTER the while loop, but inside the function. remove 4 spaces from the left of "return", not the part after "return"
Posts: 23
Threads: 10
Joined: Dec 2017
Right, I got that. Now I'm trying this, but it's giving me a different error.
import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt
from math import exp
fig, ax = plt.subplots(1, 1)
def Veganuary(StartQ_A1, EndQ_A1, CurrentQ_A1, Mean_A1, Cum_impact_A1, Peak_factor_A1):
Scale_A1=exp(Mean_A1)
return lognorm.cdf(CurrentQ_A1, Peak_factor_A1, StartQ_A1, Scale_A1)*Cum_impact_A1
def runVeganuaryMain(x):
testTime = ['48']
tTime = 48
while testTime < 400:
tTime = tTime + 4
testTime = testTime.append(str(tTime))
return Veganuary(testTime[1:37], testTime+1, x, 0.006, 0.012, 0.55)
x= np.linspace(0, 216, num=400)
ax.set_title('cumulative decrease in consumption')
ax.set_xlabel('$Quarter$')
ax.set_ylabel('$Percent of the Population$')
ax.plot(x, runVeganuaryMain(x), 'p-', lw=1, alpha=0.6, label='Major Graph') Error: Traceback (most recent call last):
File "<ipython-input-103-e0be8571f5f4>", line 1, in <module>
runfile('C:/Users/after/OneDrive/Python Scripts/DistinctionProject2017/Figure out this veganuary bs.py', wdir='C:/Users/after/OneDrive/Python Scripts/DistinctionProject2017')
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/Python Scripts/DistinctionProject2017/Figure out this veganuary bs.py", line 38, in <module>
ax.plot(x, runVeganuaryMain(x), 'p-', lw=1, alpha=0.6, label='Major Graph')
File "C:/Users/after/OneDrive/Python Scripts/DistinctionProject2017/Figure out this veganuary bs.py", line 30, in runVeganuaryMain
return Veganuary(testTime[1:37], testTime+1, x, 0.006, 0.012, 0.55)
TypeError: can only concatenate list (not "int") to list
Posts: 33
Threads: 3
Joined: Dec 2017
Dec-09-2017, 10:05 PM
(This post was last modified: Dec-09-2017, 10:07 PM by ezdev.)
testTime+1
should be testTime+[1]
or possibly: tTime+1 -- depending on what youre trying to do.
Posts: 1,150
Threads: 42
Joined: Sep 2016
Problem is in contradiction of these two expressions:
testTime = testTime.append(str(tTime))
testTime+1
Posts: 33
Threads: 3
Joined: Dec 2017
i honestly think they meant to put tTime+1 there.
|