我正在尝试将一个小区域的othographic投影保存到文件中(使用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')I'm trying to save a small area of an othographic projection to a file (using savefig()). The plot using plt.show() displays the correct area, but that saved (using plt.savefig()) shows the area of the whole disk with the desired map displayed at a small scale in the centre.
The left hand image below shows the correct area displayed. The right hand image shows the full earth's disk with the selected area shrunk in the centre.
I'd be grateful if someone could point out how I can just save the selected area to the image file.
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')最满意答案
您可以通过删除bbox_inches='tight'来解决此问题。 此设置将覆盖xlim和ylim设置,而是尝试包括图中的所有线(包括球形圆)。 查看pyplot.savefig 文档 。
You can fix this issue by removing bbox_inches='tight'. This setting will override the xlim and ylim settings, and instead tries to include all of the lines in the figure (including the globe circle). Check out the pyplot.savefig docs.
更多推荐
发布评论