GeezerVison is showing github.com — Increase axis autorange when bars have outside text labels by emilykl · Pull Request #7675 · plotly/plotly.js
Conversation
emilykl
commented
Dec 16, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Closes #2001
Adds a step during bar calc to add extra padding to axis range if bar has outside text labels.
Note: The first commit in this PR is just formatting changes. Use this diff to skip the first commit and see only the meaningful changes.
Codepen here! https://codepen.io/emilykl-code/pen/JoXQqLL
This is a stopgap fix for the fact that text labels are not taken into account during autorange calculations (see #2000).
Unfortunately, as discussed in that issue, modifying autorange to account for text labels is a significant amount of work, so I've opted for a more limited fix to address the issue identified in #2001.
Changes in this PR:
- Add a function which computes an estimate for the amount of additional axis space (in pixels) required by bar text labels
- This estimate is equal to the number of lines in the bar text (the maximum of the whole array), times the font size, plus the padding between text and bar
- This many pixels are then added to the axis range on the relevant side of the bar plot
- The extra padding is ONLY applied when all of the following are true:
textposition is 'outside', orientation is vertical, and text or texttemplate is provided and non-falsy, and textangle is 0 or 'auto'
- These exclusions are to avoid cases where the text is not parallel with the top of the bar, which are significantly harder to estimate due to needing to estimate the text width rather than the height
- The goal is to improve the current situation by adding padding in the most common cases where needed, while avoiding adding too much padding to the extent possible (even if this means excluding some cases where padding is needed)
- I am aware of one edge case where too much padding is added; this is when bars have different numbers of lines of text, but the most lines are not aligned with the longest bar. I'm willing to accept that tradeoff, but open to feedback.
- Update image baselines which show visual differences caused by this change
- Remove
"cliponaxis": "false" from the hist_stacked image mock, to demonstrate the effect of the changes in this PR (see below)
Screenshots
Before:

After:

hist_stacked mock (with cliponaxis: false removed) Before:

hist_stacked mock (with cliponaxis: false removed) After:

Steps for testing
Use the following mock:
{
"data": [
{
"type": "bar",
"orientation": "v",
"x": ["A", "B", "C"],
"y": [100, 250, -95],
"base": [100, 100, 100],
"texttemplate": "%{y}<br>units",
"textposition": "outside",
},
],
"layout": {
}
}
Verify that it looks like the "before" picture on master and the "after" picture on this branch.
Other variations to test:
- Setting
layout.yaxis.autorange to "reversed"
- Using the
bar.text property instead of bar.texttemplate
- Adding a second bar trace and setting
layout.barmode to "stack"
You can also load the hist_stacked mock in the devtools as another test case.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
emilykl
changed the title
DRAFT: Fix for cut-off bar labels
Increase axis autorange when bars have outside text labels
Dec 17, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left some syntax and refactoring comments, but this looks good.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
emilykl reacted with hooray emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
@emilykl Whereas this is potentially a breaking change for users, I'm wondering if you are going to revert this PR and introduce this change in v4?
cc: @alexcjohnson
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
IMO the previous behavior was a bug, and it's unlikely anyone depends on it, so I think including it now is the right call.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|