我试图用 pyplot 创建一个圆形抛物面(向下开口)以接近某些点,但我担心我可能缺少一些基本的东西,因为它的形状并没有。看起来没错。
相关代码如下:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.mplot3d import Axes3D
# here is a function to calculate the z for a (x,y) pair
def get_z(x,y,width,height):
# opens downward
c = - float(height)
# circular so same width in both directions
x2 = (x ** 2) / float(width)
y2 = (y ** 2) / float(width)
z = (x2+y2) / c
return(z)
# and here a function that does the plotting based on the xs and the ys
def plot_par(axes,xs,ys,zero_x=0,zero_y=0,width=1,height=100):
zs = np.array([])
for x in xs:
for y in ys:
# need to subtract the zero to center the surface
zs=np.append(zs,get_z(x-zero_x,y-zero_y,width,height))
Z = zs.reshape(len(xs),len(ys))
X,Y = np.meshgrid(xs, ys)
axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,linewidth=0, antialiased=False)
这是一个例子。我尽可能地旋转了轴,但我还是看不到抛物面;看起来其中一个轴(carga_final
)产生的是常数值:
有人在这里看到任何明显错误和/或我如何解决它?我已经尝试减去中间值(最大值+最小值/ 2),如图中所示,但这并没有解决问题。