Skip to content

[ENH]: support rect with constrained_layout ("layout only to part of the figure") #22623

Description

@anntzer

Problem

tight_layout supports a rect option ("layout axes so that they fit within this rect instead of filling the entire figure"). It could be nice if constrained_layout also supported that.

As an example use case, requested by @jklymak on gitter:

from pylab import *

fig = figure(layout="constrained")
axs = fig.subplots(1, 3)
for ax in axs:
    for i in range(3):
        ax.plot(rand(3), rand(3), label=f"dataset {i}")
axs[1].legend(ncol=3, loc="lower center", bbox_to_anchor=(0.5, 1.0))

fig = figure(layout="constrained")
axs = fig.subplots(1, 3)
for ax in axs:
    for i in range(3):
        ax.plot(rand(3), rand(3), label=f"dataset {i}")
fig.legend(handles=axs[0].lines, ncol=3, loc="upper center")

show()

This draws e.g. three different coordinates of three datasets, and tries to have a single one-line legend at the center top.

The first try gives
1
i.e. it would work if set_in_layout could be used to take the legend's y extent, but not its x extent (now that I think of it, that may be the better solution).
The second try gives
2
which is where I think a rect option would work to avoid the axes overlapping the legend (you'd have to tweak the value a bit manually, though, to specify the upper bound of the rect). (Perhaps not the nicest solution, but "could work when you just want to get a plot done".)

Proposed solution

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions