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: add supxlabel and supylabel #17524

Merged
merged 2 commits into from
Dec 3, 2020
Merged

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented May 28, 2020

PR Summary

UPDATE: 10 Oct 2020, this still works as below.

There are a couple of fixes to padding in suptitle placement in constrained_layout.

This PR adds supxlabel and supylabel. Works with tight_layout and the new constrained_layout. Not so well with no layout manager ;-)

Closes #11147

import matplotlib.pyplot as plt

for tl, cl in zip([True, False, False], [False, False, True]):
    fig = plt.figure(constrained_layout=cl, tight_layout=tl)

    gs = fig.add_gridspec(2, 3)

    ax = dict()

    ax['A'] = fig.add_subplot(gs[0, 0:2])
    ax['B'] = fig.add_subplot(gs[1, 0:2])
    ax['C'] = fig.add_subplot(gs[:, 2])

    ax['C'].set_xlabel('Booger')
    ax['B'].set_xlabel('Booger')
    ax['A'].set_ylabel('Booger Y')
    fig.suptitle(f'TEST: tight_layout={tl} constrained_layout={cl}')
    fig.supxlabel('XLAgg')
    fig.supylabel('YLAgg')

    fig.savefig(f'/Users/jklymak/downloads/suplabels_tl{tl}_cl{cl}.png', dpi=75)
    plt.show()

suplabels_tlFalse_clFalse
suplabels_tlTrue_clFalse
suplabels_tlFalse_clTrue

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@jklymak jklymak force-pushed the enh-add-suplabels branch 2 times, most recently from d53bc29 to d1a0cc4 Compare May 28, 2020 01:50
@jklymak jklymak added status: waiting for other PR topic: geometry manager LayoutEngine, Constrained layout, Tight layout labels May 28, 2020
@jklymak jklymak marked this pull request as draft May 28, 2020 02:02
@timhoffm
Copy link
Member

Have you considered that people might want suplabels on parts of a grid? e.g. columns 1 and 2 share an xlabel while column 0 has it's own label.

I'm not claiming that you should implement it now or even that it's necessary, but the desire may come up. If we consider this a reasonable extension, we should at least think about if an extension of the proposed API in that direction is possible and staight forward.

@jklymak
Copy link
Member Author

jklymak commented May 28, 2020

Right now the API is the same as suptitle, and that has the same potential to only apply to some axes and not others.

There are two somewhat orthogonal issues here.

  1. suplabels don’t align with axes, they align w/ the figure. It’s a reasonable extension to align them with the spines of the child axes, but I’ve not implemented this here. It’s non trivial to do so in a way that works with all the layout managers...
  2. How do you only label a subset of axes? I’d argue this is best served with the subpanels/subfigure concept. Then the subpanel gets the suplabels. I think we should make subpanels subclasses of figures (in some way) so they will get this change for free.

@jklymak
Copy link
Member Author

jklymak commented May 29, 2020

i.e. this figure just has suptitles for each subpanel, but was made w two subpanels, and could just as easily have supx and supylabels (from #17437)

suptitles

@jklymak jklymak mentioned this pull request Jun 15, 2020
6 tasks
@sks-2020
Copy link

Could the functions to add supxlabels and supylabels please be added to future releases of gridspec? Also have multicolumn row titles in the gridspec layout, as in the below figure?

image

@jklymak
Copy link
Member Author

jklymak commented Aug 24, 2020

@sks-2020 What do you anticipate wanting to do with gridspec that the sub-panel functionality won't give you?

@sks-2020
Copy link

@jklymak I think the subpanel function will solve what I am hoping to do. My issue is the same as outlined here:
https://stackoverflow.com/questions/40936729/matplotlib-title-spanning-two-or-any-number-of-subplot-columns

I found a possible way of solving this, but doesn't use the neat layout option of using gridspec:
https://stackoverflow.com/questions/27426668/row-titles-for-matplotlib-subplot?rq=1

In summary, I have a gridspec subplots layout within another gridspec. I do the plotting part in the inner grid and need the labeling flexibility in the outer grid. At the moment, using the current release of matplotlib, I can only achieve this using text annotations. Inbuilt functions (like supxlabel, supylabel, supxticklabels, supyticklabels, suptitle), for the outer grid, will be very useful.

@jklymak
Copy link
Member Author

jklymak commented Sep 16, 2020

@mwaskom This is the suplabel PR. This doesn't really require the other PR, but would be nice to do it after....

@mwaskom
Copy link

mwaskom commented Sep 16, 2020

I'm afraid I don't know where to find it in the diff, but is the default font size here bigger than regular axes labels?

I'm not sure I've ever needed to add a "suplabel" pertaining to multiple axes that have existing, distinct labels. My use-case is almost always a situation where I have multiple subplots with shared axes and I want to add a single label to identify them. In that case, I'd want to the label to have the same size as a normal axis label.

Also, will these suplabels be tighter against the axes when there are no first-order labels?

I won't assert or claim to know which is a more common usecase, but I do think it's an application this PR should consider...

@jklymak
Copy link
Member Author

jklymak commented Sep 16, 2020

I think they may be larger by default (just like suptitle) but the user can change this.

If there are no individual labels and you use constrained_layout the suplabels will be tight against the axes. Can likely be made to work with tight_layout as well. With no layout manager the whole thing is a bit of a mess anyhow.

As noted above, the suplables will be centered on the figure, not the spines or some other measure of what the label is supposed to span. That may or may not be acceptable depending on axes decorations, but it's a lot simpler.

@jklymak jklymak force-pushed the enh-add-suplabels branch 2 times, most recently from e2d9131 to acc550a Compare October 10, 2020 04:06
@jklymak jklymak marked this pull request as ready for review October 10, 2020 04:16
@jklymak
Copy link
Member Author

jklymak commented Oct 14, 2020

This is ready for review, and closes at least one feature request, and something seaborn would like.

@dopplershift dopplershift added this to the v3.4.0 milestone Oct 14, 2020
doc/users/next_whats_new/suplabels.rst Outdated Show resolved Hide resolved
doc/users/next_whats_new/suplabels.rst Show resolved Hide resolved
examples/subplots_axes_and_figures/figure_title.py Outdated Show resolved Hide resolved
doc/users/next_whats_new/suplabels.rst Outdated Show resolved Hide resolved
examples/subplots_axes_and_figures/figure_title.py Outdated Show resolved Hide resolved
lib/matplotlib/tests/test_figure.py Outdated Show resolved Hide resolved
lib/matplotlib/tests/test_figure.py Outdated Show resolved Hide resolved
lib/matplotlib/tight_layout.py Outdated Show resolved Hide resolved
lib/matplotlib/_constrained_layout.py Show resolved Hide resolved
doc/users/next_whats_new/suplabels.rst Outdated Show resolved Hide resolved
@jklymak jklymak force-pushed the enh-add-suplabels branch 2 times, most recently from dfe0086 to 2c51392 Compare November 10, 2020 17:19
@jklymak jklymak force-pushed the enh-add-suplabels branch 3 times, most recently from e5e4813 to dfd9ed0 Compare November 18, 2020 21:27
@jklymak jklymak force-pushed the enh-add-suplabels branch 2 times, most recently from b5a9247 to 04b736f Compare November 18, 2020 21:29
examples/subplots_axes_and_figures/figure_title.py Outdated Show resolved Hide resolved
examples/subplots_axes_and_figures/figure_title.py Outdated Show resolved Hide resolved
lib/matplotlib/_constrained_layout.py Outdated Show resolved Hide resolved
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
@dopplershift
Copy link
Contributor

Were we relying on Travis to run any unique test configurations? I'm wondering why test lines execute has dropped so much...

@QuLogic
Copy link
Member

QuLogic commented Nov 24, 2020

Is it not posting? It seems to have run...

Not everything is installed in Azure Pipelines, so I don't think it covers the same things.

@dopplershift
Copy link
Contributor

Travis CI doesn't show up in the current list of checks on this PR. It looks like no PR builds have run in 3 days. I'm assuming we're running into the same thing that SciPy and NumPy have run into due to Travis changing their pricing model:

image

@QuLogic
Copy link
Member

QuLogic commented Nov 24, 2020

Oh, I did not notice the date. I guess we'll put this back on the meeting agenda.

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very nice looking!

I also really like the test images, they make it very clear what is going on.

@tacaswell
Copy link
Member

Anyone can merge on CI green.

@QuLogic QuLogic merged commit ff15ca9 into matplotlib:master Dec 3, 2020
@QuLogic
Copy link
Member

QuLogic commented Dec 3, 2020

Code coverage is normal again, since this ran with GitHub Actions after @tacaswell's commit.

@jklymak
Copy link
Member Author

jklymak commented Dec 3, 2020

Thanks for everyone's reviews, and for @QuLogic for fixing codecov!

@jklymak jklymak deleted the enh-add-suplabels branch December 3, 2020 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: geometry manager LayoutEngine, Constrained layout, Tight layout
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: add a supxlabel and supylabel as the suptitle function which are already exist
7 participants