Issue
I have found an example somewhere of how to use the Ogrid function and have been playing around with different values. Now to use it, I need the imaginary part/ step length to be calculated from array parameters. Therefore the following question:
Why does this work:
Working lines of code
xi, yi = np.ogrid[0:1:10j, 0:1:10j]
and this doesn't:
Not working lines of code
rows = str(10) + "j"
columns = str(10) + "j"
xi, yi = np.ogrid[0:1:rows, 0:1:columns]
in the following program:
Entire sample
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import griddata as gd
# define X Y V testsample
x = [0, 1, 1, 0]
y = [0, 0, 1, 1]
v = [1, 2, 3, 4]
# create grid
# Not working example
rows = str(10) + "j"
columns = str(10) + "j"
xi, yi = np.ogrid[0:1:rows, 0:1:columns]
# Working example
##xi, yi = np.ogrid[0:1:10j, 0:1:10j]
X1 = xi.reshape(xi.shape[0])
Y1 = yi.reshape(yi.shape[1])
ar_len = len(X1)*len(Y1)
X = np.arange(ar_len, dtype=float)
Y = np.arange(ar_len, dtype=float)
l = 0
for i in range(0, len(X1)):
for j in range(0, len(Y1)):
X[l] = X1[i]
Y[l] = Y1[j]
l+=1
#interpolate v on xy grid
V = gd((x,y), v, (X,Y), method='linear')
print(V)
#Plot original values
fig1 = plt.figure()
ax1=fig1.gca()
sc1=ax1.scatter(x, y, c=v, cmap=plt.hot())
plt.colorbar(sc1)
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
#Plot interpolated values
fig2 = plt.figure()
ax2=fig2.gca()
sc2=ax2.scatter(X, Y, c=V, cmap=plt.hot())
plt.colorbar(sc2)
ax2.set_xlabel('X')
ax2.set_ylabel('Y')
#ax2.set_zlabel('Z')
plt.show()
Try:
I have already tried the following to figure out what is happening. I created a variable, changed it to a string and add a "j" to it. It is equal to "10j" but not equal to 10j. I think this difference causes the problem, but I do not know how to convert a string to an imaginary number. I also tried np.imag(), but this only returns the numeral from the imaginary part of a complex number, so without the j.
a = str(10) + "j"
print(a)
print(a==10j) # Results in False
print(a=='10j') # Results in True
Solution
if you want to write your code like the way you showed it in the non working example do this :
# An update to the not working example
rows = 10 * 1j
columns = 10 * 1j
xi, yi = np.ogrid[0:1:rows, 0:1:columns]
otherwise, if you really want to change a string into a complex number, use the eval()
method like :
x = eval('10j')
print(type(x))
output:
<class 'complex'>
Answered By - mrCopiCat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.