Question about calling a function that calls a function - 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: Question about calling a function that calls a function (/thread-6831.html) |
Question about calling a function that calls a function - Afterdarkreader - Dec-09-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
RE: Question about calling a function that calls a function - hshivaraj - Dec-09-2017 def runVeganuaryMain(x): testTime = 52 while testTime < 400: Veganuary(testTime, testTime+1, x, 0.006, 0.012, 0.55) testTime = testTime + 4 returnThis function seems to return nothing?! Shouldn't it be the y axis values? RE: Question about calling a function that calls a function - Afterdarkreader - Dec-09-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 RE: Question about calling a function that calls a function - hshivaraj - Dec-09-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 Graphthe 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 RE: Question about calling a function that calls a function - j.crater - Dec-09-2017 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? RE: Question about calling a function that calls a function - ezdev - Dec-09-2017 (Dec-09-2017, 09:50 PM)Afterdarkreader Wrote: see but if I do this: 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" RE: Question about calling a function that calls a function - Afterdarkreader - Dec-09-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')
RE: Question about calling a function that calls a function - ezdev - Dec-09-2017 testTime+1 should be testTime+[1] or possibly: tTime+1 -- depending on what youre trying to do. RE: Question about calling a function that calls a function - j.crater - Dec-09-2017 Problem is in contradiction of these two expressions: testTime = testTime.append(str(tTime)) testTime+1 RE: Question about calling a function that calls a function - ezdev - Dec-09-2017 i honestly think they meant to put tTime+1 there. |