【问题标题】:matplotlib basemap othographic projection saved file covers different area from displayed onematplotlib底图正交投影保存的文件覆盖与显示的不同区域
【发布时间】:2016-04-24 16:47:14
【问题描述】:

我正在尝试将正射投影的一小部分区域保存到文件中(使用 savefig())。使用 plt.show() 的绘图显示正确的区域,但保存的(使用 plt.savefig())显示整个磁盘的区域,所需的地图在中心以小比例显示。

下面的左图显示了正确的显示区域。右侧的图像显示了整个地球的圆盘,其中选定的区域在中心缩小了。

如果有人能指出如何将所选区域保存到图像文件中,我将不胜感激。

from mpl_toolkits.basemap import Basemap
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.lines as lines 
import numpy as np


lon_0 = 5.0
lat_0 = 45.0

max_lat = lat_0 + 10.0
min_lat = lat_0 - 10.0
max_lon = lon_0 + 15.0
min_lon = lon_0 - 15.0

plt.figure(figsize=(6,6))   
my_map = Basemap(projection='ortho', lon_0=lon_0, lat_0=lat_0, resolution='l')

my_map.drawcoastlines()
my_map.drawcountries()
my_map.drawparallels(np.arange(-90.,95.,5.))
my_map.drawmeridians(np.arange(0.,365.,5.))

xmin, ymin = my_map(min_lon, min_lat)
xmax, ymax = my_map(max_lon, max_lat)

my_map.plot( [xmin, xmax], [ymin, ymax], marker=None,color='m')  # plot a cross
my_map.plot( [xmin, xmax], [ymax, ymin], marker=None,color='m')

ax = plt.gca()          # set the axes limits
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)


output_file = 'example_a.png'
plt.savefig( output_file, bbox_inches='tight', dpi=20)        # save image

plt.show()

plt.close('all')

【问题讨论】:

    标签: python matplotlib projection


    【解决方案1】:

    您可以通过删除 bbox_inches='tight' 来解决此问题。此设置将覆盖xlimylim 设置,而是尝试包括图中的所有线条(包括地球圆)。查看pyplot.savefigdocs

    【讨论】:

    • z.fox 非常感谢您为我指明了正确的方向 - 现在可以正常工作了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-19
    • 2019-03-13
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2014-02-10
    相关资源
    最近更新 更多