I have a function called funcPower3 with body presented below. I would like to plot this function with 3D plot functionnalities in MatplotLib. I saw an example on scipy docs with meshgrid. However, in this example, function is not a defined function, but a simple mathematical operation:
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,
linewidth=0, antialiased=False)
I take inspiration in this sample, but my own function signature is not compatible with data out of meshgrid. I have this error:
ValueError: zero-size array to minimum.reduce without identity
My function's code is here:
def funcPower3(PARAM):
inputX1 = open("power3X.txt","r")
inputY = open("power3Y.txt","r")
X1=[]
Y=[]
for line in inputX1:
X1.append(float(line))
for line in inputY:
Y.append(float(line))
resTmp_ = 0
res = 0
for i in range(len(X1)):
resTmp_ = Y[i] - (PARAM[0]*(X1[i])**float(PARAM[1]))
res += resTmp_**2
return res
And code to plot 3d this function is here:
xmin = 0 ymin = 0 xmax = 10 ymax = 1
fig = plt.figure('Power 3')
ax = fig.gca(projection='3d')
Z = []
Xpl = numpy.arange(xmin, xmax, 0.1).tolist()
Ypl = numpy.arange(ymin, ymax, 0.01).tolist()
Xpl, Ypl = numpy.meshgrid(Xpl, Ypl)
Z=[]
for i in range(len(Xpl[0])):
for j in range(len(Xpl)):
Z.append(funcPower3([Xpl[j][i],Ypl[j][i]]))
surf=ax.plot_surface(Xpl, Ypl, Z, rstride=8, cstride=8, alpha=0.3,cmap=cm.jet)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
plt.show()
Thanks and regards for any advice; ))