3

我正在尝试熟悉 matplotlib 和 Basemap。首先,我正在尝试生成与我拥有数据的特定网格相匹配的格陵兰图像。

下面令人毛骨悚然的细节描述了我的问题:我无法创建与所需投影/区域匹配的正确尺寸的图像。

我想匹配的投影和网格: Projection as Proj4 string:"+proj=stere +lat_0=90 +lon_0=-45 +lat_ts=70 +ellps=WGS84 +datum=WGS84 +units=m"

由网格定义的区域是一个 800x1400 2000m 分辨率的网格,其中:LowerLeft Corner 的外边缘(m):-700,000.,-3,400,000。右上角的外边缘 (m):900,000.,-600,000。=> (-700,000 + 2000 * 800, -3,400,000 + 2000 * 1400)

底图不允许我为立体投影指定以米 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 的图像。如果我没有bbox_inches='tight'plt.savefig命令中包含 ,我会得到一个 800x1400 的图像,沿着(编辑)底部边缘(编辑)有一条不可见的像素。

任何人都可以帮助我,以便我可以确定我正在正确设置我的底图吗?我觉得我可能只是错过了一个简单的技巧,但没有得到我期望的大小的图像很奇怪。

与往常一样,提前致谢。

4

1 回答 1

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()
于 2012-11-16T20:32:51.680 回答