2015-11-06 68 views
0

我試圖創建一個圓形拋物面(向下開放)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)中的一個產生恆定的值:

enter image description here

是否有人看到什麼明顯的錯誤在這裏和/或我怎麼能解決這個問題?我試圖減去中間值(最大值+最小值/ 2),就像你在圖像中看到的那樣,但是這並沒有解決問題。

回答

1

它工作正常,問題是因爲你的x和y軸的長度不同。

例如,使用下面的行(x = 0-100, y = 0-500)這似乎符合你軸限制,則在y方向上得到一個稍微彎曲的表面:

plot_par(ax,np.arange(0,100,1),np.arange(0,500,5),zero_x=50, zero_y=250) 

enter image description here

但是,如果你的xy範圍是相同的,它看起來更好:

plot_par(ax,np.arange(0,100,1),np.arange(0,100,1),zero_x=50, zero_y=50) 

enter image description here

+0

有道理。感謝您的解釋。 –