Skip to content

[ENH]: Add test for annotate(textcoods="offset fontsize") #25907

Closed

Description

@anntzer

Problem

annotate() currently supports (in particular) specifying the position of the text in "offset points" and "offset pixels" relative to the position of the point being annotated.
It would be nice to also support "offset font" (perhaps spelt "offset fontsize", but I'm not sure the extra verbosity helps), which would be a offset but in units of the fontsize (of the annotation text). This is useful e.g. when using annotate() to generate subplot labels (#25905), where it makes sense to say "I want to put the label at the top right of the axes, offset from the corner by half-a-fontsize rightwards and downwards" (xytext=(.5, -.5), xycoords="offset font").

Proposed solution

No response


As noted below this is actually already present (and has now been documented as such), but adding tests would be nice.

Activity

  1. story645 commented on May 17, 2023

    @story645
    Member

    Would this make it easier to do multicolor text?

  2. anntzer commented on May 18, 2023

    @anntzer
    ContributorAuthor

    No, because that requires measuring the actual width of the glyphs, which depends on the actual text.

    I guess it may be a good idea, though, to rewrite the rainbow_text example using annotate(), further taking advantage of annotate()'s ability to take a Artist (i.e., its extent) as transform: each word can just be positioned at the bottom right corner relative to the previous one. (Support for the vertical text case is left as an exercise to the reader.)
    (Note in particular that the current version of rainbow_text seems, AFAICT, not robust against dpi changes; the approach proposed above may be better in that respect.)

  3. saranti commented on May 19, 2023

    @saranti
    Contributor

    Isn't that what this fontsize parameter does?
    https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/text.py#L1510

    e.g:

    xytext=(0.5, 0.5), textcoords='offset fontsize'
  4. oscargus commented on May 19, 2023

    @oscargus
    Member

    Good find! It at least seems like the fontsize option is not documented at all. It is probably also possible to use offset fraction if I do not read the code wrong?

    (For a while, I thought that OffsetFrom was involved, which only supports points and pixels, so maybe there is a historical the reason for not documenting offset fraction? I guess that it should be possible to add fontsize and fraction to OffsetFrom that is valid when passing an Artist (of the correct type)?)

  5. oscargus commented on May 19, 2023

    @oscargus
    Member

    It is not tested though, so good actions here are probably to add a test to make sure that it does what it appears to, and to add documentation for it (as well as offset fraction). Maybe, update an example with it as well.

    A secondary task may be to extend OffsetFrom to handle fraction and fontsize in case of an Artist. Although one can just as well open an separate issue for that. Edit: OffsetFrom is not tested at all, although there are examples that uses it.

  6. saranti commented on May 20, 2023

    @saranti
    Contributor

    offset fraction is returning an AttributeError at the moment because the bbox isn't being passed to bbox0. I was able to get it working by passing it here on line 1500 but it seems more like a hack. I also don't know if it's supposed to be a fraction of the figure or the axes?

    elif bbox_name == "offset":
    xy0 = self._get_position_xy(renderer)

  7. oscargus commented on May 20, 2023

    @oscargus
    Member

    You are correct. Didn't think that far, just looked at the possible combinations. One would need offset axes fraction etc, but probably not worth it.

  8. anntzer commented on May 21, 2023

    @anntzer
    ContributorAuthor

    This should mostly be handled by #25940, modulo tests.

  9. niranjank2022 commented on May 24, 2023

    @niranjank2022
    Contributor

    No, because that requires measuring the actual width of the glyphs, which depends on the actual text.

    I guess it may be a good idea, though, to rewrite the rainbow_text example using annotate(), further taking advantage of annotate()'s ability to take a Artist (i.e., its extent) as transform: each word can just be positioned at the bottom right corner relative to the previous one. (Support for the vertical text case is left as an exercise to the reader.) (Note in particular that the current version of rainbow_text seems, AFAICT, not robust against dpi changes; the approach proposed above may be better in that respect.)

    "
    annotate()'s ability to take a Artist (i.e., its extent) as transform:
    "
    I don't understand what are you trying to say by this. Do we have to create Artist object (matplotlib.artist.Artist) with required properties and assign it to the keyword argument transform?

  10. changed the title [-][ENH]: Support "offset font" as textcoords in annotate()[/-] [+][ENH]: Add test for annotate(textcoods="offset fontsize")[/+] on May 26, 2023
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