Skip to content

Axis label alignment defaults #15839

Closed
Closed

Description

@andrzejnovak

Feature Request

Would there be opposition to adding x, y, ha, va as rcparams? I would like aligning labels to the end of axes to be a style option.
Or really a shortcut for set_label(), i.e. align "left" : <x/y>=0, ha='left' and "left" : <x/y>=1, ha='right' and align could be a new rcparam?

Activity

  1. jklymak commented on Dec 5, 2019

    @jklymak
    Member

    Can you clarify your proposed API? I'm not even clear what method you want these to apply to...

  2. andrzejnovak commented on Dec 5, 2019

    @andrzejnovak
    ContributorAuthor

    Sure, set_xlabel() currently takes ha='right', x=1 which effectively aligns label to the right. I would like this to be an optional default i.e. rcparam, though that could quickly get bloated ha/x/y for xaxis/yaxis. So alternatively set_xlabel() could take a new kwarg align={"right"|"center"|"left"} which would be a shortcut for setting the right ha/x/y and then align could be a singular new rcparam.

  3. timhoffm commented on Dec 7, 2019

    @timhoffm
    Member

    The idea is to align the axis label left/center/right with respect to the axis.

    (horizontal/vertical)align unfortunately has a fixed notion to align the text relative to a reference point. Therefore adding a parameter align may be a source of confusion.

    The desired effect is a bit analogous to the parameter loc in Axes.set_title. However, the difference is, that there are actually 3 titles which may be used simultaneously, and loc selects which title to use. OTOH legend() actually uses loc to move the legend. So set_xlabel('the label', loc='left') may after all be the way to go even if we just have one Text object for the label.

  4. andrzejnovak commented on Dec 7, 2019

    @andrzejnovak
    ContributorAuthor

    I don't have an issue with loc instead of align such that set_xlabel('the label', loc='left') where 'left' means set_xlabel('the label', ha='right', x=1) and similarly with 'right'. I'm happy to take a crack at a PR if we can get a consensus on the API.

  5. jklymak commented on Dec 7, 2019

    @jklymak
    Member

    If x=1 is already the way to do this maybe we capture x and allow special string values for it. Ie x=“left”. It would also allow us to document that you can just pass x to set_xlabel, whivc I hadn’t realized before now.

  6. andrzejnovak commented on Dec 7, 2019

    @andrzejnovak
    ContributorAuthor

    @jklymak Unfortunately for set_ylabel() it's y not x. The goal here is to have a setup that will allow for an rcParam like axis.label.loc = 'right' that will align all x-label, y-label, cbar-label to the right.

  7. timhoffm commented on Dec 8, 2019

    @timhoffm
    Member

    Technically x/y is documented via the Text properties in kwargs (https://matplotlib.org/devdocs/api/_as_gen/matplotlib.axes.Axes.set_xlabel.html).

    I'm in favor of a new loc='left' instead of x='left'. On the downside, we have another parameter that interacts with x and horizontalalignment. But also x='left' would still interact with horizontalalignment. We cannot get around that completely. Then IMHO it is clearer to introduce a separate loc parameter as a high-level interface that manipulates the low-level interface (x/horizontalalignment).

    There's also some layouting happening in the non-axis direction, which overrides any y parameter given to xlabel:

    l = plt.xlabel('test')
    print(l)  # Text(0.5, 0, 'test')
    plt.draw()
    print(l)  # Text(0.5, 15.0, 'test')
    

    If we pull x to top level in xlabel, people will get aware of y, but that does not work for setting the vertical position, which we would have to doc as well. Also for that reason, I don't think we should use or advertise x.

  8. andrzejnovak commented on Dec 8, 2019

    @andrzejnovak
    ContributorAuthor

    Could also babyproof it, i.e throw error if both x/ha and loc get used

  9. anntzer commented on Dec 8, 2019

    @anntzer
    Contributor

    I think @timhoffm's point is a good one in favor of loc="left" and against x="left".

  10. QuLogic commented on Jul 23, 2020

    @QuLogic
    Member

    This was added in #15974.

  11. added this to the v3.3.0 milestone on Jul 23, 2020
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