matplotlib basemap othographic投影保存的文件覆盖了显示的不同区域(matplotlib basemap othographic projection saved file

编程入门 行业动态 更新时间:2024-10-26 05:18:39
matplotlib basemap othographic投影保存的文件覆盖了显示的不同区域(matplotlib basemap othographic projection saved file covers different area from displayed one)

我正在尝试将一个小区域的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.

更多推荐

本文发布于:2023-07-04 11:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023622.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区域   文件   othographic   basemap   matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!