X Tutup
Skip to content

get_geometry() wrong if subplots are nested (e.g., subplots with colorbars) #7225

@nschloe

Description

@nschloe

Note: This is from a stackoverflow question.


Consider

import numpy as np
import matplotlib.pyplot as plt

data = np.zeros((3, 3))

fig = plt.figure()

ax1 = plt.subplot(131)
ax2 = plt.subplot(132)
ax3 = plt.subplot(133)
axes = [ax1, ax2, ax3]

for ax in axes:
    im = ax.imshow(data)

The resulting figure fig has 3 AxesSubplot children, one for each subplot. Their geometry

for child in fig.get_children():
    try:
        print(child.get_geometry())
    except AttributeError:
        pass
(1, 3, 1)
(1, 3, 2)
(1, 3, 3)

makes sense.

Now let's add some color bars:

for ax in axes:
    fig.colorbar(im, ax=ax, orientation='horizontal')

Now the geometries are reported as

(2, 1, 1)
(2, 1, 1)
(2, 1, 1)
(1, 3, 2)
(1, 3, 2)
(1, 3, 2)

All information about how the plots are arranged seems lost.

I guess I would have expected a nested structure: The three plots are children of fig with the ordering
(1, 3, 1), (1, 3, 2), (1, 3, 3), and each of those has two subplots with the ordering (2, 1, 1), (2, 1, 2).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup