79527450

Date: 2025-03-22 12:42:07
Score: 1
Natty:
Report link

Based on the approach from @raphael, I adjusted my code that is makes use of a sub-grid.


def render_heatmaps():
    # Create a figure with multiple subplots
    nrows = len(domain_knowledge["DS1: Automotive"])
    ncols = len(domain_knowledge.keys())
    gs = fig.add_gridspec(3, 1)  # Adjust the width ratios
    gs.set_height_ratios([0.7, 4, 0.1])

    # setup the sub-grid for the plot axes
    heatmaps_gs = GridSpecFromSubplotSpec(
        nrows, ncols, gs[1, 0], hspace=0.07, wspace=0.02
    )

    # Render infobox
    ax_text: Axes = fig.add_subplot(gs[0, :])
    render_infobox(ax_text)

    # Create a colorbar based on the min/max values that are in all datasets (contingency matrices for each study object)
    norm = mcolors.Normalize(vmin=np.min(datasets), vmax=np.max(datasets))
    colors = ScalarMappable(norm, cmap="GnBu")

    for row in range(nrows):
        for col in range(ncols):
            ax = fig.add_subplot(heatmaps_gs[row, col])
           [... same code...]

    # create a combined colorbar
    cax = fig.add_subplot(gs[2, :])  # Use all columns for the colorbar
    fig.colorbar(colors, cax=cax, orientation="horizontal")
    cax.set_title("Number of Study Responses")

Looks pretty much like I wanted it:

enter image description hereBut since i've seen the result from @Jody's solution, I wonder if it's possible to add a bit padding around the colorbar subplot/ axis (cax) as well?
If somebody knows how to do that, would be great if you could comment and I will adjust the final solution.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @raphael
  • User mentioned (0): @Jody's
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Mayor Mayer