2014-06-30 46 views
1

是否有一種簡單的方法可以通過PyPlot以對數方式縮放顫抖函數繪製的箭頭的長度?用pyplot繪製顫動函數的箭頭的對數長度

原因是,我在海上和陸地上繪製了風箭,而海洋上方的風力大約高出十倍。

因此,無論是土地上的箭頭太小,無法獲得任何信息來繪製它們,或者海洋上的箭頭太大,導致情節太過擁擠在海洋上。

我已經嘗試過使用symlog函數來對數據進行縮放,但它看起來像角度會造成麻煩。爲了強調這一點,這裏是一小塊代碼:使用此數據,保存在test.txt

# lattitude longitude u v 
2.145047503739818495e+01 -8.062500000000000000e+01 -5.790064811706542969e+00 2.341136932373046875e-01 
2.145047503739818495e+01 -7.312500000000000000e+01 -7.119166374206542969e+00 -1.223894119262695312e+00 
2.145047503739818495e+01 -6.562500000000000000e+01 -6.140162467956542969e+00 -1.082292556762695312e+00 
2.145047503739818495e+01 -5.812500000000000000e+01 -4.589381217956542969e+00 2.853832244873046875e-01 
2.145047503739818495e+01 -5.062500000000000000e+01 -5.221705436706542969e+00 4.221019744873046875e-01 
2.145047503739818495e+01 -4.312500000000000000e+01 -5.333521842956542969e+00 6.321525573730468750e-02 
1.771996152644742750e+01 -2.812500000000000000e+01 -7.793482780456542969e+00 -3.714616775512695312e+00 
1.771996152644742750e+01 -2.062500000000000000e+01 -6.195338249206542969e+00 -6.160417556762695312e+00 
1.585470386969487322e+01 -7.687500000000000000e+01 -8.054713249206542969e+00 -1.638355255126953125e-01 
1.585470386969487322e+01 -6.937500000000000000e+01 -7.378443717956542969e+00 -2.906990051269531250e-02 
1.585470386969487322e+01 -6.187500000000000000e+01 -6.270533561706542969e+00 3.127269744873046875e-01 
1.585470386969487322e+01 -5.437500000000000000e+01 -7.410181999206542969e+00 -1.028003692626953125e-01 
1.212418712345576566e+01 -3.937500000000000000e+01 -8.221217155456542969e+00 -2.800065994262695312e+00 
1.212418712345576566e+01 -3.187500000000000000e+01 -7.579127311706542969e+00 -3.560319900512695312e+00 
1.212418712345576566e+01 -2.437500000000000000e+01 -5.761256217956542969e+00 -5.112565994262695312e+00 
1.025892816800637597e+01 -8.062500000000000000e+01 -4.789576530456542969e+00 -4.363542556762695312e+00 
1.025892816800637597e+01 -7.312500000000000000e+01 -1.818385124206542969e+00 -1.677995681762695312e+00 
1.025892816800637597e+01 -6.562500000000000000e+01 -3.078639030456542969e+00 -1.554460525512695312e+00 
6.528409401479990493e+00 -5.062500000000000000e+01 -7.822779655456542969e+00 -1.683855056762695312e+00 
6.528409401479990493e+00 -4.312500000000000000e+01 -8.200709342956542969e+00 -2.835222244262695312e+00 
6.528409401479990493e+00 -3.562500000000000000e+01 -7.456568717956542969e+00 -2.850358963012695312e+00 
6.528409401479990493e+00 -2.812500000000000000e+01 -5.878443717956542969e+00 -2.700944900512695312e+00 
6.528409401479990493e+00 -2.062500000000000000e+01 -2.720240592956542969e+00 -1.258562088012695312e+00 
4.663149706177883935e+00 -7.687500000000000000e+01 1.257298469543457031e+00 -1.143815994262695312e+00 
9.326299678379910141e-01 -6.187500000000000000e+01 -6.386976242065429688e-01 -2.507495880126953125e-01 
9.326299678379910141e-01 -5.437500000000000000e+01 -2.149439811706542969e+00 -1.390886306762695312e+00 
9.326299678379910141e-01 -4.687500000000000000e+01 -5.939478874206542969e+00 -1.460222244262695312e+00 
9.326299678379910141e-01 -3.937500000000000000e+01 -6.882838249206542969e+00 -5.959644317626953125e-01 
9.326299678379910141e-01 -3.187500000000000000e+01 -6.343287467956542969e+00 -1.565113067626953125e-01 
9.326299678379910141e-01 -2.437500000000000000e+01 -5.749537467956542969e+00 3.444652557373046875e-01 
-4.663149706177883935e+00 -7.312500000000000000e+01 -7.033824920654296875e-02 -3.654956817626953125e-01 
-4.663149706177883935e+00 -6.562500000000000000e+01 -5.674085617065429688e-01 -3.176441192626953125e-01 
-4.663149706177883935e+00 -5.812500000000000000e+01 -1.063014030456542969e+00 -1.213550567626953125e-01 
-4.663149706177883935e+00 -5.062500000000000000e+01 -1.417994499206542969e+00 -2.028980255126953125e-01 
-4.663149706177883935e+00 -4.312500000000000000e+01 -1.486842155456542969e+00 -8.557300567626953125e-01 
-4.663149706177883935e+00 -3.562500000000000000e+01 -5.729517936706542969e+00 1.451887130737304688e+00 
-8.393668907692383385e+00 -2.062500000000000000e+01 -6.579615592956542969e+00 2.450422286987304688e+00 
-1.025892816800637597e+01 -7.687500000000000000e+01 4.018297195434570312e-01 -1.755542755126953125e-01 
-1.025892816800637597e+01 -6.937500000000000000e+01 2.187242507934570312e-01 -5.783863067626953125e-01 
-1.025892816800637597e+01 -6.187500000000000000e+01 -3.462171554565429688e-01 -8.737964630126953125e-01 
-1.025892816800637597e+01 -5.437500000000000000e+01 -4.731702804565429688e-01 -3.200855255126953125e-01 
-1.025892816800637597e+01 -4.687500000000000000e+01 -8.545179367065429688e-01 -4.890308380126953125e-01 
-1.398944571235667311e+01 -3.187500000000000000e+01 -7.993678092956542969e+00 1.462230682373046875e-01 
-1.398944571235667311e+01 -2.437500000000000000e+01 -8.063502311706542969e+00 1.512434005737304688e+00 
-1.585470386969487322e+01 -8.062500000000000000e+01 -2.528346061706542969e+00 3.198469161987304688e+00 
-1.585470386969487322e+01 -7.312500000000000000e+01 2.924547195434570312e-01 1.297590255737304688e+00 
-1.585470386969487322e+01 -6.562500000000000000e+01 -3.598890304565429688e-01 -1.232194900512695312e+00 
-1.585470386969487322e+01 -5.812500000000000000e+01 4.196643829345703125e-02 -1.524187088012695312e+00 
-1.958521860882233057e+01 -4.312500000000000000e+01 -1.116724967956542969e+00 -3.195972442626953125e-01 
-1.958521860882233057e+01 -3.562500000000000000e+01 -5.567896842956542969e+00 -3.761003494262695312e+00 
-1.958521860882233057e+01 -2.812500000000000000e+01 -7.378443717956542969e+00 -1.043718338012695312e+00 
-1.958521860882233057e+01 -2.062500000000000000e+01 -8.015162467956542969e+00 7.102775573730468750e-02 
-2.145047503739818495e+01 -7.687500000000000000e+01 2.352025032043457031e+00 2.376691818237304688e+00 
-2.145047503739818495e+01 -6.937500000000000000e+01 2.939915657043457031e+00 1.054914474487304688e+00 

def main(): 
    lat, lon, u, v = readmulticol2Dfile('test.txt', shape=(9,6)) 
    # map without logarithmic scaling 
    plt.subplot(1,2,1) 
    mapproj = bm.Basemap(projection='cyl', llcrnrlon=lon.min(), urcrnrlon=lon.max(), llcrnrlat=lat.min(), urcrnrlat=lat.max()) 
    mapproj.drawcoastlines() 
    mapproj.drawparallels(np.linspace(-20,20,5), labels=[1,0,0,0]) 
    mapproj.drawmeridians(np.linspace(-80,20,5), labels=[0,0,0,1]) 
    plt.title('linear') 
    plt.quiver(lon, lat, u, v, color='k', units='x') 
    # map with logarithmic scaling 
    plt.subplot(1,2,2) 
    mapproj = bm.Basemap(projection='cyl', llcrnrlon=lon.min(), urcrnrlon=lon.max(), llcrnrlat=lat.min(), urcrnrlat=lat.max()) 
    mapproj.drawcoastlines() 
    mapproj.drawparallels(np.linspace(-20,20,5), labels=[1,0,0,0]) 
    mapproj.drawmeridians(np.linspace(-80,20,5), labels=[0,0,0,1]) 
    plt.quiver(lon, lat, symlog(u), symlog(v), color='k', units='x') 
    plt.title('logarithmic') 
    plt.show() 

def readmulticol2Dfile(fname, header=True, delimiter='\t', shape=None): 
    """reads a multicolumn txt-file and converts it to numpy arrays""" 
    a=np.loadtxt(fname).T 
    if shape is not None: 
    b=[np.zeros(shape=shape)]*4 
    for i in xrange(len(a)): b[i]=np.reshape(a[i],shape) 
    return b 
    else: return a 

def symlog(x): 
    """ Returns the symmetric log10 value """ 
    return np.sign(x) * np.log10(np.abs(x)) 

if __name__=="__main__": 
    main() 

但不幸的是結果看起來很奇怪。實際上長度實際上是對數的,但在某些地區這些角度表現很奇怪。

非常感謝您的幫助提前=)

PS:我沒有足夠的信譽來添加一個數字或第二個超鏈接這裏,所以我上傳的FTP服務器在那裏將後會自動取消了兩週。您可以通過在上面的數據鏈路中用'.png'替換'.txt'來簡單地訪問該圖。

回答

1

從我收集你可以只路過它們之前縮放箭頭的長度。這是

import matplotlib.pyplot as plt 
import numpy as np 

def symlog(x): 
    """ Returns the symmetric log10 value """ 
    return np.sign(x) * np.log10(np.abs(x)) 

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10,5)) 

# Generate fake data 
N = 50 
X,Y = np.meshgrid(np.linspace(0, 1, N), np.linspace(0, 1, N)) 
U = np.random.normal(0, 0.2, size=(50, 50)) 
V = np.random.normal(0, 0.2, size=(50, 50)) 

# Set RHS data to be larger than LHS 
U[:, N/2:] *= 1000 
V[:, N/2:] *= 1000 
angles=np.arctan2(V,U)*180.0/np.pi # calculate angles manually 

#Without scaling 
HEAD_LENGTH = 4 
Q = ax1.quiver(X[::3, ::3], Y[::3, ::3], U[::3, ::3], V[::3, ::3], 
      color='k', units='x', headaxislength=HEAD_LENGTH) 

#With scaling 
Q = ax2.quiver(X[::3, ::3], Y[::3, ::3], symlog(U[::3, ::3]), symlog(V[::3, ::3]), 
      color='k', units='x', headaxislength=HEAD_LENGTH, angles=angles) 

plt.show() 

enter image description here

在左邊顯示的原始數據,而右邊我有一個例子使用symlog()縮放箭頭長度。這會在保留其符號的同時縮放值的大小,否則會記錄負數的錯誤。

+0

嘿,謝謝你的幫助! :)雖然它對你有用,但對數似乎會導致角度出現問題。我用一個使用simlog功能的例子編輯我的文章(參見上文)。你能用我的數據和腳本重現我的問題嗎? – Chilipp

+0

我懷疑它,因爲我沒有你的數據文件。請添加一個簡短的完整工作示例,例如複製和粘貼生成的繪圖。這將有助於改善你得到的答案。 – Greg

+1

好吧,我通過手動計算角度並使用顫抖圖的關鍵字'角度'來製作角度。我提供了下面的完整代碼。 – Chilipp