2012-11-12 35 views
3

我正在嘗試用matplotlib和底圖對自己進行歸類。作爲一個開始,我試圖生成一個格陵蘭的圖像,該圖像與我擁有數據的特定網格匹配。創建與地球上特定區域相對應的簡單底圖

下面這些令人毛骨悚然的細節描述了我的問題:我無法創建與所需投影/區域匹配的圖像。

的投影和電網,我想匹配: 投影爲Proj4字符串:"+proj=stere +lat_0=90 +lon_0=-45 +lat_ts=70 +ellps=WGS84 +datum=WGS84 +units=m"

通過網格所限定的區域是800x14002000米分辨率網格其中:LowerLeft角落(米)的 外緣: -700,000,-3,400,000。 UpperRight Corner(m)的外邊緣:900,000,-600,000。 =>(-700,000 + 2000 * 800,-3,400,000 + 2000 * 1400)

底圖不會讓我指定以米爲單位的角度,因此我必須將這些轉換爲緯度/經度。

> gdaltransform -s_srs "+proj=stere +lat_0=90 +lon_0=-45 +lat_ts=70 +ellps=WGS84 +datum=WGS84 +units=m" -t_srs "+proj=latlong"` 
-700000 -3400000 
-56.6336339989404 58.7244253840871 0 
900000 -600000 
11.3099324740202 80.0389929796586 0 

現在我應該有所有的信息來創建一個800x1400圖像。

from mpl_toolkits.basemap import Basemap 
import matplotlib.pyplot as plt 

def create_map(): 
    fig = plt.figure(1, figsize=(8, 14), frameon=False, dpi=100) 
    fig.add_axes([0, 0, 1, 1]) 

    m = Basemap(resolution="i", 
       projection='stere', lat_ts=70, lat_0=90., lon_0=-45., 
       llcrnrlon=-56.6336339989404, llcrnrlat=58.7244253840871, 
       urcrnrlon=11.3099324740202, urcrnrlat=80.0389929796586, 
       rsphere=(6378137.0, 6356752.3142)) 

    m.drawcoastlines() 
    m.fillcontinents(color='#c1c1c1') 
    m.drawmapboundary(fill_color='#6587ad', linewidth=0.0) 
    plt.savefig('greenland.png', pad_inches=0.0, bbox_inches='tight') 

if __name__ == '__main__': 
    create_map() 

我面對的問題是,當我這樣做時,我得到一個800x1399的圖像。如果我在plt.savefig命令中沒有包含bbox_inches='tight',我會得到一張800x1400的圖像,沿着(編輯)底邊(編輯)顯示一條不可見的像素。

任何人都可以幫助我,這樣我就可以確定我正確設置了我的底圖嗎?我覺得我可能只是錯過了一個簡單的訣竅,但沒有得到我期望的大小的圖像很奇怪。

一如既往,在此先感謝。

回答

1

看來,這可能是matplotlib中的一個錯誤的結果。傑夫惠特克看了看,並說它看起來是正確的,我試圖重現這種行爲,而不使用底圖,我能夠。

似乎數據值的方面可能會導致輸出圖像大小錯誤。

下面是一些顯示問題的代碼。抱歉虛驚一場。

# rectangle.py -- 
import matplotlib.pyplot as plt 


def create_image(): 
    fig = plt.figure(1, figsize=(8, 14), frameon=False, dpi=100) 
    fig.add_axes([0, 0, 1, 1]) 
    ax = plt.gca() 

    # This isn't necessary to create the issue unless you want to see the 
    # transparent pixels at bottom. 

    # for spine in ax.spines.values(): 
    #  spine.set_linewidth(0.0) 

    limb = ax.axesPatch 
    limb.set_facecolor('#6587ad') 

    x1 = 0.0 
    y1 = 0.0 
    x2 = 16. 

    # Use this line and get what I was expecting: 
    # y2 = 27.999999999999994671 # produces 800 x 1400 image 

    # Use this line and get the wrong size 
    y2 = 27.999999999999994670 # produces (wrong?) 800 x 1399 image 

    corners = ((x1, y1), (x2, y2)) 
    ax.update_datalim(corners) 
    ax.set_xlim((x1, x2)) 
    ax.set_ylim((y1, y2)) 

    ax.set_aspect('equal', anchor='C') 
    ax.set_xticks([]) 
    ax.set_yticks([]) 

    plt.savefig('rectangle.png', pad_inches=0.0, bbox_inches='tight') 

    # If you use this below, the file size is correct, but there is a single 
    # line transparent pixels along the bottom of the image if you set the 
    # linewidth to zero... 

    # plt.savefig('rectangle.png', pad_inches=0.0) 


if __name__ == '__main__': 
    create_image() 
相關問題