Skip to content

[Bug]: colorbar doesn't register inset_axis as cax  #27763

@ericasaw

Description

@ericasaw

Bug summary

When constructing inset axes using the tutorial on this page the gradient of the colorbar does not appear in the constructed inset axis but somewhere else on the figure (location seems to depend on figure size) once saved to a PDF with multiple other figures if bbox_inches='tight'. Similar to #27143 and #25176.

Code for reproduction

from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import matplotlib.pylab as plt
from matplotlib.backends.backend_pdf import PdfPages

#make the first figure
fig, axes = plt.subplots(1, 1, facecolor = 'white', figsize = (20, 5), constrained_layout = True)

x = random.randint(100, size=(100, 2048))
c = axes.imshow(x, origin = 'lower', cmap = 'Reds', vmin = 0, vmax = 100, aspect = "auto")
axins1 = inset_axes(axes,
                    width="10%",
                    height="30%",
                    loc="upper left",
                    bbox_to_anchor = (0.045, 0., 1, 1),
                    bbox_transform = axes.transAxes
                    )
axins1.xaxis.set_ticks_position("bottom")
fig.colorbar(c, cax=axins1, orientation="horizontal")
axes.set_yticklabels([])

fig.suptitle("Title")
plt.figtext(-0.02, 0.5, 'Random', c = 'k', rotation = 90, ma = 'center', fontsize = medium)

#make figure 2
fig2, axes = plt.subplots(1, 1, facecolor = 'white', figsize = (20, 5), constrained_layout = True)

x = random.randint(100, size=(100, 2048))
c = axes.imshow(x, origin = 'lower', cmap = 'Reds', vmin = 0, vmax = 100, aspect = "auto")
axins1 = inset_axes(axes,
                    width="10%",
                    height="30%",
                    loc="upper left",
                    bbox_to_anchor = (0.045, 0., 1, 1),
                    bbox_transform = axes.transAxes
                    )
axins1.xaxis.set_ticks_position("bottom")
fig2.colorbar(c, cax=axins1, orientation="horizontal")
axes.set_yticklabels([])

fig2.suptitle("Title")
plt.figtext(-0.02, 0.5, 'Random', c = 'k', rotation = 90, ma = 'center', fontsize = medium)

#save figs to PDF
figs = [fig, fig2]
with PdfPages('test.pdf') as pdf:
    for fig in figs:
        pdf.savefig(fig, bbox_inches='tight')

Actual outcome

The gradient for the colorbar appears outside of the inset axes on each of the plots
test_small.pdf

Expected outcome

The result for this particular example when bbox_inches='tight' is not called in savefig(). Note: when running in jupyter lab this is always the outcome--only when saving the figure does the gradient jump out of the inset axis.
test_loose.pdf

Additional information

I tried the same suggestion as in #27143 and used axins1 = axes.inset_axes([0.05, 0.7, 0.1, 0.2], transform = axes.transAxes) in my own code which seemed to fix this. Unsure why the example constructor for inset_axes here doesn't work in this case.

Operating system

Mac OS 13.6.3 (Ventura) Intel

Matplotlib Version

3.8.0

Matplotlib Backend

Installed osx event loop hook. MacOSX

Python version

Python 3.9.18

Jupyter version

Jupyter Lab 3.6.3

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions