Matplotlib 创建与地球上特定区域相对应的简单底图
我正试图让自己熟悉matplotlib和Basemap。首先,我尝试生成一个格陵兰岛的图像,该图像与我有数据的特定网格相匹配 下面可怕的细节描述了我的问题:我无法创建与所需投影/面积匹配的适当大小的图像 我要匹配的投影和栅格: 投影为项目4字符串:Matplotlib 创建与地球上特定区域相对应的简单底图,matplotlib,gdal,matplotlib-basemap,Matplotlib,Gdal,Matplotlib Basemap,我正试图让自己熟悉matplotlib和Basemap。首先,我尝试生成一个格陵兰岛的图像,该图像与我有数据的特定网格相匹配 下面可怕的细节描述了我的问题:我无法创建与所需投影/面积匹配的适当大小的图像 我要匹配的投影和栅格: 投影为项目4字符串:“+proj=stere+lat_0=90+lon_0=-45+lat_ts=70+ellps=WGS84+datum=WGS84+units=m” 由栅格定义的区域是分辨率为800x1400 2000m的栅格,其中: 下角外缘(m):-700000.
“+proj=stere+lat_0=90+lon_0=-45+lat_ts=70+ellps=WGS84+datum=WGS84+units=m”
由栅格定义的区域是分辨率为800x1400 2000m的栅格,其中:
下角外缘(m):-700000.,-3400000。
右上角外缘(m):900000.,-600000.=>(-700000+2000*800,-3400000+2000*1400)
Basemap不允许我为赤平投影指定以米xy为单位的角点,因此我必须将这些角点转换为lat/lon
> 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\u inches='tight'
,我会得到一张800x1400的图像,在(编辑)底边(编辑)有一条不可见的像素
有人能帮我确定我的底图设置正确吗?我觉得我可能只是错过了一个简单的技巧,但没有得到我期望的图像大小是很奇怪的
一如既往,请提前感谢。看来这可能是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()