Nested For Loop And 3d Plot Within Class Object
Solution 1:
The problem is that self.z
is not a two-dimensional array/list. Therefore, trying to access self.z[k][l]
results in IndexError: invalid index to scalar variable.
I do not quite understand how you want to implement the second dimension. You introduce the y-position, but then, you just calculate a 1D radius array by using both the x- and y-location in
self.r = np.sqrt((x-self.xlage)**2+(y-self.ylage)**2)
The next question is, what do you intend with:
self.P[self.xlage] = np.nan self.P[self.ylage] = np.nan
If you change
xsteps
andysteps
to10
, and call:b1 = Bohrloch(2,3,0*24*3600,6.0/1000) print b1.getPressure(t)
Your output will be:
[ 5.44152501 4.40905986 nan nan 2.87481753 2.64950827 2.46756653 2.31503845 2.18379093 2.06866598]
Why would you want to replace the 3rd and 4th elements with
nan
?
These issues are also at the basis of your plotting routine. Because you now have np.nan
values in your array, these won't show in the plot. Because self.z
is not two-dimensional, you are probably not getting the surface you may be expecting:
Here's a simple way of coming up with a 2D implementation. I am not familiar enough with what you are trying to do, but it gets the idea across:
defgetPressure(self, t):
if (t-self.tstart<0):
return ()
print"Startpunkt liegt außerhalb des Förderzeitraumes!"else:
# you need to initialize r, P and z as list of lists# make this dependent on your x coordinates# the second dimension will grow dynamically
self.r = [[] for ri inrange(len(x))]
self.P = [[] for ri inrange(len(x))]
self.z = [[] for ri inrange(len(x))]
# iterate through both x and y independentlyfor ii inrange(len(x)):
for jj inrange(len(y)):
# append to the list that corresponds to the current x -value# also, use x[ii] and y[jj] to call one x-, y-value at a time
self.r[ii].append(np.sqrt((x[ii]-self.xlage)**2+(y[jj]-self.ylage)**2))
# calling r[ii][-1] ensures you are using the value that was last added to the list:
self.P[ii].append((self.q/(rhof*4*pi*kappa))*(expn(1,self.r[ii][-1]**2/(4*c*(t-self.tstart)))))
self.z[ii].append(self.P[ii][-1]/1e6)
# now, you can use xlage and ylage to blank one value# do this for both P and z, because z is now calculated inside the loop
self.P[self.xlage][self.ylage] = np.nan
self.z[self.xlage][self.ylage] = np.nan
return self.z
From your plotting routine, remove this line: Z[Z == np.inf] = np.nan
, use your original command:
b1 = Bohrloch(50,50,0*24*3600,6.0/1000)
b1.pressurePlot3D(t)
and you will now get this plot:
Post a Comment for "Nested For Loop And 3d Plot Within Class Object"