Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mathause opened this issue Jun 4, 2024 · 1 comment · Fixed by #28525
Closed

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

mathause opened this issue Jun 4, 2024 · 1 comment · Fixed by #28525

Comments

@mathause
Copy link
Contributor

mathause commented Jun 4, 2024

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

@mathause mathause changed the title [Bug]: incosistent colorbar spacing for ImageGrid with cbar_mode="single" [Bug]: incosistent colorbar pad for ImageGrid with cbar_mode="single" Jun 4, 2024
@anntzer
Copy link
Contributor

anntzer commented Jun 13, 2024

This seems like a clear bug with no obvious backcompat route; I would suggest changing to not including axes_pad when positioning the colorbar and having a clear note in the API changelog.

@QuLogic QuLogic added this to the v3.10.0 milestone Jul 13, 2024
@ksunden ksunden changed the title [Bug]: incosistent colorbar pad for ImageGrid with cbar_mode="single" [Bug]: inconsistent colorbar pad for ImageGrid with cbar_mode="single" Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants