Han Zhengzu
Han Zhengzu

Reputation: 3852

Setting plot border frame for two subplot containing MatPlotLib.Basemap contents

As an illustration, I present a figure here to depict my question.

fig = plt.figure(figsize = (10,4))
ax1 = plt.subplot(121)
map =Basemap(llcrnrlon=x_map1,llcrnrlat=y_map1,urcrnrlon=x_map2,urcrnrlat=y_map2)
map.readshapefile('shapefile','shapefile',zorder =2,linewidth = 0)
for info, shape in zip(map.shapefile, map.shapefile):
    x, y = zip(*shape) 
    map.plot(x, y, marker=None,color='k',linewidth = 0.5)
plt.title("a")
ax2 = plt.subplot(122)
y_pos = [0.5,1,]
performance = [484.0,1080.0]
bars = plt.bar(y_pos, performance, align='center')
plt.title("b")

enter image description here

Due to the mapping setting is not consistent with the subplot(b). Thus, subplot(a) and subplot(b) has distinct board frame. In my opinion, the un-aligned borders are not pleasant for reader.

Is there any way to adjust the boarder size of subplot(b) in order to harmony as a whole figure.

This is my target:

enter image description here

Notice that, subplot(a) need to contain matplotlib.basemap elements.

Upvotes: 0

Views: 541

Answers (1)

Vlas Sokolov
Vlas Sokolov

Reputation: 3893

Currently, your subplot on the left has an 'equal' aspect ratio, while for the other one it is automatic. Therefore, you have to manually set the aspect ratio of the subplot on the right:

def get_aspect(ax):
    xlim = ax.get_xlim()
    ylim = ax.get_ylim()
    aspect_ratio = abs((ylim[0]-ylim[1]) / (xlim[0]-xlim[1]))

    return aspect_ratio

ax2.set_aspect(get_aspect(ax1) / get_aspect(ax2))

Upvotes: 1

Related Questions