Skip to content

[Bug]: Axes.label_outer() does not work when there is a colorbar #27305

Open
@chrisyeh96

Description

@chrisyeh96

Bug summary

Axes.label_outer() fails to hide axis and tick labels for plots that have a colorbar.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
cl2c = axs[0, 1].scatter(x, y, c=x)
fig.colorbar(cl2c, ax=axs[0, 1], label='c-label')
axs[1, 0].plot(x, y)
axs[1, 1].plot(x, y)

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# try to hide x labels and tick labels for top plots and y ticks for right plots,
# but this fails to hide the labels for axs[0, 1] due to its colorbar
for ax in axs.flat:
    ax.label_outer()

Actual outcome

The output of the code above gives:

image

Expected outcome

I would have expected the plot to look like:

image

This expected plot was created by creating the colorbar after the ax.label_outer() calls. However, I would not have expected this to have affected whether the labels/ticks are removed.

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
cl2c = axs[0, 1].scatter(x, y, c=x)
axs[1, 0].plot(x, y)
axs[1, 1].plot(x, y)

for ax in axs.flat:
    ax.set(xlabel='x-label', ylabel='y-label')

# Hide x labels and tick labels for top plots and y ticks for right plots.
for ax in axs.flat:
    ax.label_outer()

fig.colorbar(cl2c, ax=axs[0, 1], label='c-label')

fig.tight_layout()

Additional information

No response

Operating system

Ubuntu 20.04.5 LTS

Matplotlib Version

3.8.0

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

Python 3.11.6

Jupyter version

7.0.6

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions