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
[Bug]: Axes.bar_label() on log scale does not center the label. #23688
When plotting on a log scale, if you pass label_type='center' to Axes.bar_label() the resulting labels are not centered in the bar, since the labels are being centered using the original base 10 scale.
This only seems to work if the x in xy is 1, as I can move the text vertically by changing the y and the result is as expected. However, if I change the x value to 0.5 to place it halfway along the bar, nothing shows up, and if I do 0.99 the text is way beyond the beginning of the x-axis:
Oh, that makes sense. The bounding bax it is using is not clipped so the left edge is way off the the left at where ever the very small number we clip 0 up to in log scale is. Same log problem, but from the other side.....
Thanks @anntzer – I'm glad my debugging helped get to the bottom of this. Can I put in a PR to update Axes.bar_label() to properly handle the label_type='center' going forward? Unless this is something you don't want in the API, it seems more robust than what is already there and I imagine the logic will work for any axis scale.
Activity
tacaswell commentedon Aug 21, 2022
https://matplotlib.org/stable/tutorials/text/annotations.html#using-complex-coordinates-with-annotations I think you want to use option 2 in that section and place the text relative to the bounding box of the rectangle.
The back-compatibility on this may require some thought.
stefmolin commentedon Aug 21, 2022
@tacaswell - Did you mean something like this?
This only seems to work if the

xinxyis 1, as I can move the text vertically by changing theyand the result is as expected. However, if I change thexvalue to 0.5 to place it halfway along the bar, nothing shows up, and if I do 0.99 the text is way beyond the beginning of the x-axis:tacaswell commentedon Aug 21, 2022
Oh, that makes sense. The bounding bax it is using is not clipped so the left edge is way off the the left at where ever the very small number we clip 0 up to in log scale is. Same log problem, but from the other side.....
anntzer commentedon Aug 21, 2022
The following (which was made easy by your debugging above!) seems to work:
stefmolin commentedon Aug 21, 2022
Thanks @anntzer – I'm glad my debugging helped get to the bottom of this. Can I put in a PR to update
Axes.bar_label()to properly handle thelabel_type='center'going forward? Unless this is something you don't want in the API, it seems more robust than what is already there and I imagine the logic will work for any axis scale.anntzer commentedon Aug 21, 2022
Seems good to me (although I admittedly haven't followed the whole discussion).
Implement solution from matplotlib#23688 to properly center bar label…
Implement solution from matplotlib#23688 to properly center bar label…