You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[ENH]: Add test for annotate(textcoods="offset fontsize") #25907
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.
Reacted by Thomas A Caswell, hannah, Tim Hoffmann and Ruth Comer
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.)
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)?)
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.
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?
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?
changed the title [-][ENH]: Support "offset font" as textcoords in annotate()[/-][+][ENH]: Add test for annotate(textcoods="offset fontsize")[/+]on May 26, 2023
Activity
story645 commentedon May 17, 2023
Would this make it easier to do multicolor text?
anntzer commentedon May 18, 2023
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.)
saranti commentedon May 19, 2023
Isn't that what this
fontsizeparameter does?https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/text.py#L1510
e.g:
oscargus commentedon May 19, 2023
Good find! It at least seems like the
fontsizeoption is not documented at all. It is probably also possible to useoffset fractionif I do not read the code wrong?(For a while, I thought that
OffsetFromwas involved, which only supportspointsandpixels, so maybe there is a historical the reason for not documentingoffset fraction? I guess that it should be possible to addfontsizeandfractiontoOffsetFromthat is valid when passing anArtist(of the correct type)?)oscargus commentedon May 19, 2023
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
OffsetFromto handlefractionandfontsizein case of anArtist. Although one can just as well open an separate issue for that. Edit:OffsetFromis not tested at all, although there are examples that uses it.saranti commentedon May 20, 2023
offset fractionis returning an AttributeError at the moment because the bbox isn't being passed tobbox0. 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?oscargus commentedon May 20, 2023
You are correct. Didn't think that far, just looked at the possible combinations. One would need
offset axes fractionetc, but probably not worth it.anntzer commentedon May 21, 2023
This should mostly be handled by #25940, modulo tests.
niranjank2022 commentedon May 24, 2023
"
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?
[-][ENH]: Support "offset font" as textcoords in annotate()[/-][+][ENH]: Add test for annotate(textcoods="offset fontsize")[/+]