Skip to content
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

[ENH]: Ignore ticklabels in colorbar constrained-positioning #26014

Open
anntzer opened this issue May 31, 2023 · 6 comments
Open

[ENH]: Ignore ticklabels in colorbar constrained-positioning #26014

anntzer opened this issue May 31, 2023 · 6 comments
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues New feature topic: color/colorbar topic: geometry manager LayoutEngine, Constrained layout, Tight layout

Comments

@anntzer
Copy link
Contributor

anntzer commented May 31, 2023

Problem

Sometimes, trying to stick a colorbar next to its "parent" axes by using pad=0 will "fail" if the axes has a ticklabel that extends beyond the axes' spines:

from pylab import *
fig = figure(layout="constrained", figsize=(8, 4))
axs = fig.subplots(1, 2)
for i, ax in enumerate(axs):
    im = ax.imshow(np.arange(9).reshape((3, 3)), aspect="auto")
    fig.colorbar(im, pad=0, ticks=[])
axs[0].set(xlim=(-.5, 2.5), xticks=[0, 1, 2], yticks=[])
axs[1].set(xlim=(0, 3), xticks=[0, 1, 2, 3], yticks=[])
show()

test
see the small extra spacing between the left axes and the left colorbar, not present on the right.

Proposed solution

I'm not sure this can reliably be handled automatically (the desirable behavior also depends on whether the colorbar's vertical extent goes goes beyond the axes' vertical extent, which will depend on how exactly the colorbar is positioned, whether the parent axes uses fixed aspect (here I disabled fixed aspect ratio on imshow).

But it would be nice if one could at least tweak that manually with something like axs[1].xaxis.set_in_layout(False). Bonus points if one can do set_in_layout("y"), i.e. take the xaxis in account but only for its vertical extent (we don't want the labels to fall off below the figure), not for its horizontal extent.

@anntzer anntzer added New feature topic: geometry manager LayoutEngine, Constrained layout, Tight layout topic: color/colorbar labels May 31, 2023
@timhoffm timhoffm added the Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues label May 31, 2023
@timhoffm
Copy link
Member

As a rough guess this is medium difficulty.

@jklymak
Copy link
Member

jklymak commented Jun 1, 2023

There are different paths here. The set_inlayout('x') route is probably fine and not very hard

Somehow doing this all the time is a bad idea. People have long ticklabels and you dont want those spilling over into the next axes.

Somehow detecting when it is wanted because I have a colorbar adjacent seems very hard

If I wanted the colorbar to hang exactly off the axes I would use inset_axes

@anntzer
Copy link
Contributor Author

anntzer commented Jun 1, 2023

As noted above I agree doing this automatically is not particularly reasonable.

@oliviavanzandt
Copy link

@anntzer Can I be assigned this issue to work on?

@QuLogic
Copy link
Member

QuLogic commented Dec 1, 2023

We don't assign issues; if you want to work on this, then adding a note here is all you need to do.

@jayyxiao
Copy link

jayyxiao commented Apr 1, 2024

I will try this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues New feature topic: color/colorbar topic: geometry manager LayoutEngine, Constrained layout, Tight layout
Projects
None yet
Development

No branches or pull requests

6 participants