Skip to content Skip to sidebar Skip to footer

Adding Legend In Geopandas Plot With Subplots Changes Size Of Plot

I want to plot two GeoPandas plots with matplotlib subplots. The two maps have the same legend, and therefore I only want to have one legend. However, if I add a legend to one of t

Solution 1:

In order to reproduce you issue, I used this code, which basically shows the same result: the image on the right is slightly smaller than the left one due to the colorbar.

import matplotlib.pyplotas plt
import numpy as np

D2012 = np.random.rand(10, 10)
D2013 = np.random.rand(10, 10)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (16,8))

P2012 = ax1.imshow(D2012,
             cmap = 'magma')
P2013 = ax2.imshow(D2013,
             cmap = 'magma')

ax1.set_title('2012', fontsize = 18)
ax2.set_title('2013', fontsize = 18)
ax1.axis('off')
ax2.axis('off')
plt.colorbar(P2013)

plt.show()

which gives this plot:

enter image description here

I solved with this turnaround:

import matplotlib.pyplotas plt
import numpy as np

D2012 = np.random.rand(10, 10)
D2013 = np.random.rand(10, 10)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (16,8))
ax3 = fig.add_axes([0.85, 0.1, 0.1, 0.8])

P2012 = ax1.imshow(D2012,
             cmap = 'magma')
P2013 = ax2.imshow(D2013,
             cmap = 'magma')

ax1.set_title('2012', fontsize = 18)
ax2.set_title('2013', fontsize = 18)
ax1.axis('off')
ax2.axis('off')
ax3.axis('off')
plt.colorbar(P2013, ax = ax3)

plt.show()

which gives this plot:

enter image description here

Basically, I add a third axis, turn it off and add to it the colorbar. You need to pay attention to the position of this third axis with the parameters inside the method: fig.add_axes([0.85, 0.1, 0.1, 0.8]). I know this is not the most elegant solution, for sure.


EDIT

A more robust and elegant solution is to keep 2 axes, but set their size and position when you define them:

import matplotlib.pyplotas plt
import numpy as np

D2012 = np.random.rand(10, 10)
D2013 = np.random.rand(10, 10)

fig = plt.figure(figsize = (16,8))
ax1 = fig.add_axes([0, 0.2, 0.6, 0.6])
ax2 = fig.add_axes([0.4, 0.2, 0.6, 0.6])

P2012 = ax1.imshow(D2012,
             cmap = 'magma')
P2013 = ax2.imshow(D2013,
             cmap = 'magma')

ax1.set_title('2012', fontsize = 18)
ax2.set_title('2013', fontsize = 18)
ax1.axis('off')
ax2.axis('off')
plt.colorbar(P2013)

plt.show()

which gives this plot:

enter image description here

In this case you have to pay attention to the position and the size of the two axis with these lines:

ax1 = fig.add_axes([0, 0.2, 0.6, 0.6])
ax2 = fig.add_axes([0.4, 0.2, 0.6, 0.6])

Solution 2:

One approach is to create an exclusive axes for plotting the color bar. So 3 axes are created for 2 required plots. Note the use of gridspec_kw with proper values of width_ratios to obtain required widths of the subplots. Here is the runnable code and the resulting plot.

import numpy as np
import matplotlib.pyplot as plt

# create simple data for demo plot
data = np.arange(100, 0, -1).reshape(10, 10)

# figsize plays important role; must set it wide enough# here 3 axes are created# the width ratio of the 3rd axes must be small to get good result (here = 0.03)
fig, ax = plt.subplots(1, 3, figsize=(9.5, 4), gridspec_kw={"width_ratios":[0.4535, 0.4535, 0.03]})

# first 2 axes are used to plot images
im0 = ax[0].imshow(data, cmap='bone')
im1 = ax[1].imshow(data, cmap='bone')

# The third axes, ax[2] is exclusive to the color bar# When cax id specified, 'shrink' and 'aspect' properties are ignored
cb = fig.colorbar(im0, cax=ax[2], orientation='vertical')

plt.show()

With this approach, the color bar can be placed on any of the 3 axes, but you have to change the code accordingly to get it done.

enter image description here

Post a Comment for "Adding Legend In Geopandas Plot With Subplots Changes Size Of Plot"