2012-11-12 4 views
3

matplotlib 및 Basemap에 자신을 심기 위해 노력하고 있습니다. 우선 데이터가있는 특정 그리드와 일치하는 그린 랜드 이미지를 생성하려고합니다.지구상의 특정 지역에 해당하는 간단한베이스 맵 만들기

아래의 끔찍한 내용은 내 문제를 설명합니다. 원하는 투영 면적과 일치하는 적절한 크기의 이미지를 만들 수 없습니다.

I 일치하고자 투영 그리드 : LowerLeft 코너 (m)의 외연 : "+proj=stere +lat_0=90 +lon_0=-45 +lat_ts=70 +ellps=WGS84 +datum=WGS84 +units=m"

그리드에 의해 정의 된 영역은 800x1400 2,000m 해상도 눈금 : Proj4 문자열로 투영 : -700,000., -3,400,000. 어퍼 라이트 코너 (m)의 바깥 쪽 가장자리 : 900,000., -600,000. => (-700,000 + 2000 * 800, -3,400,000 + 2000 * 1400)

Basemap은 위도/경도로 변환해야하므로 stereographic 투영을 위해 meters-xy의 모서리를 지정할 수 없습니다.

> 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의 버그로 인한 것 같습니다. 제프 휘태커 (Jeff Whitaker)가 한 번 훑어 보았고 바르게 보인다고 말했고 Basemap을 사용하지 않고이 동작을 재현하려고 시도했습니다.

데이터 값의 측면에서 출력 이미지의 크기가 잘못되었을 수 있습니다.

다음은 문제를 나타내는 코드입니다. 거짓 경보로 유감입니다.

# 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() 
관련 문제