KirbyJames
KirbyJames

Reputation: 75

matplotlib basemap othographic projection saved file covers different area from displayed one

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.Left: Displayed Image, Right: Image saved to png 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')

Upvotes: 0

Views: 1655

Answers (1)

Zach Fox
Zach Fox

Reputation: 614

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.

Upvotes: 2

Related Questions