6

I use geopandas and matplotlib.pyplot's subplots to plot two subplots in a single figure, with a single colorbar, as:

two subplots with wasted spaces around

How do I reduce the whitespace around the maps in each subplot (not in between subplots - I know how to do that)?

The relevant code is:

import geopandas as gpd
import matplotlib.pyplot as plt

side_length = 10
test_data = gpd.read_file(shpfile)
fig, axes = plt.subplots(1, 2, figsize=(side_length * 2.1, side_length))
fig.subplots_adjust(hspace=0.5, wspace=0.5)

# 1st subplot
test_data['data'] = data1 # assume data is in a list data1
ax = test_data.plot(column='data', axes=axes.flat[0], cmap=cmap, edgecolor='gray', vmin=vmin, vmax=vmax)
ax.set_axis_off()
ax.set_aspect('equal')

# 2nd subplot
test_data['data'] = data1 # assume data is in a list data2
ax = test_data.plot(column='data', axes=axes.flat[1], cmap=cmap, edgecolor='gray', vmin=vmin, vmax=vmax)
ax.set_axis_off()
ax.set_aspect('equal')

# Add single colorbar
cax = fig.add_axes([0.9, 0.17, 0.02, 0.65])
sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=vmin, vmax=vmax))
sm._A = []
cbar = fig.colorbar(sm, cax=cax)
cbar.ax.set_title('Number of Cases')

fig.savefig('myFig.pdf', bbox_inches='tight', pad_inches=0.1)
0

1 Answer 1

5

The asker found their own answer:

I got the answer: just reduce spaces in subplots_adjust as:

fig.subplots_adjust(hspace=0.0, wspace=0.0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.