pythonmatplotlibmatplotlib-gridspec

Aligning a table with title in a GridSpec


I have the following page layout with two charts up top, a map, and a table.

As you can see in the result, the table title is not aligned with the Map title, and there is way too much space between bottom of Fig 1 and top of Map 1. Ideally I want the map and the table to be aligned (vertically) and with the Map and Table titles also aligned.

How would I make that happen?

Result

import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from blume.table import table

fig = plt.figure(figsize=(11, 8.5))
fig.suptitle(f"Water intensity of {ti}")
gs = gridspec.GridSpec(2, 2, height_ratios=[1, 2])
ax = fig.add_subplot(gs[0, 0])
ax.set_title("Fig 1")
ax = fig.add_subplot(gs[0, 1])
ax.set_title("Fig 2")
ax = fig.add_subplot(gs[1, 0])
ax.set_title("Map 1")
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

world.plot(ax=ax)
ax = fig.add_subplot(gs[1, 1])
ax.set_axis_off()
tab = table(ax,
    cellText=["10","20"],
    colLabels=[1,2],
    rowLabels=['a','b'],
    loc='center'
).scale(1,1.5)
ax.set_title("Table 1")

Solution

  • The table position could be adjusted upwards by using loc='upper center' instead of loc='center'.

    To fix the map position within its subplot, add ax.set_aspect('equal', anchor='N') after ax.set_title("Map 1").

    The resulting page layout: enter image description here