Skip to content

[Bug]: inconsistent colorbar pad for ImageGrid with cbar_mode="single" #28343

@mathause

Description

@mathause

Bug summary

ImageGrid with cbar_mode="single" adds the axes_pad between the axes and the colorbar if the colorbar is to the left or at the bottom. That seems inconsistent and unnecessary as the space can be controlled with cbar_pad.

Code for reproduction

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid


fig = plt.figure(figsize=(6, 6))

subfigs = fig.subfigures(2, 2, wspace=0.07, hspace=0.1, linewidth=1)
figs = subfigs.flatten()

locations = ["left", "bottom", "right", "top"]

for fig, location in zip(figs, locations):

    fig.set_edgecolor("r")

    axgr = ImageGrid(
        fig,
        111,
        nrows_ncols=(1, 1),
        axes_pad=1. / 2.54,
        cbar_mode="single",
        cbar_location=location,
        cbar_pad=0. / 2.54,
    )
    ax = axgr.axes_all[0]
    ax.text(0.5, 0.5, f"{location=}", transform=ax.transAxes, va="center", ha="center")
    ax.set(xticks=[], yticks=[])
    
    cbax = axgr.cbar_axes[0]
    cbax.set(xticks=[], yticks=[])

    plt.savefig("cbar_mode_single.png")

Actual outcome

cbar_mode_single

Expected outcome

I expect all colorbars to hug the axes.

Additional information

  • As mentioned this only happens for cbar_mode="single" and cbar_location "left" or "bottom"
  • This behavior seems to have been around since forever.
  • There is the cbar_pad param which can be used to control the distance between axes and colorbar
  • Note that both cbar_pad and axes_pad are added between the axes and the colorbar
  • I am happy to open to open a PR, but don't see a way to deprecate this gracefully

Operating system

No response

Matplotlib Version

3.8.4

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions