Skip to content

bar chart: textposition='auto' not working for 0 values #6474

Description

@gochristoph

Screenshot 2022-09-23 at 17 54 08

From the textposition argument documentation for bar traces:

"auto" tries to position text inside the bar, but if the bar is too small and no bar is stacked on this one the text is moved outside.

For small values this is working nicely: Instead of inside, the text gets positioned outside.

But: For 0 values, the text disappears completely instead of being moved outside.

import pandas as pd
import plotly.express as px

df = pd.DataFrame({
    "Country": [f"Country {n}" for n in range(6)],
    "Some Metric": [8,5,5,.5,0.0001,0]
})

fig = px.bar(
    df, 
    x='Some Metric',
    y='Country', 
    text='Country',
    orientation='h')
fig.update_yaxes(visible=False, showticklabels=False)

# change here between "auto" (default), "inside", "outisde"
fig.update_traces(textposition="auto")

fig.show()

Setting 0 values to a small value is a dirty fix that may also lead to the text disappearing when the graph is resized. At the same time, such a fix endangers data integrity.

The alternative would be setting the textposition for every observation separately. I think that should be avoided and covered by setting texposition="auto".

Fix that everybody can implement in their code currently:

fig.update_traces(textposition=['outside' if val==0 else 'auto' for val in df['Some Metric']])

Tested with plotly 5.10.0.

Activity

  1. AaronStiff commented on Feb 8, 2023

    @AaronStiff
  2. AaronStiff commented on Feb 8, 2023

    @AaronStiff

    @archmoj is this by design? I noticed this in bar/plot.js:

        if(!text ||
            textPosition === 'none' ||
            ((calcBar.isBlank || x0 === x1 || y0 === y1) && (
                textPosition === 'auto' ||
                textPosition === 'inside'))) {
            bar.select('text').remove();
            return;

    if(!text ||
    textPosition === 'none' ||
    ((calcBar.isBlank || x0 === x1 || y0 === y1) && (
    textPosition === 'auto' ||
    textPosition === 'inside'))) {
    bar.select('text').remove();
    return;

  3. transferred this issue fromplotly/plotly.pyon Feb 8, 2023
  4. alexcjohnson commented on Feb 9, 2023

    @alexcjohnson
    Collaborator

    Does look like a bug to me, and certainly a zero-size bar is different from no bar! Nice detective work @AaronStiff - fixing this may be as simple as removing that textPosition === 'auto' || line (and adding this situation to a test image) but it would be good to at least investigate why that condition was added in the first place...

  5. AaronStiff commented on Feb 9, 2023

    @AaronStiff

    Thanks @alexcjohnson! Unfortunately I'm not really setup for/knowledgeable in plotly.js development yet, so I'll let someone else make that change. But the git blame seems like it's been around for several years.

  6. JosieAilsa commented on Aug 8, 2023

    @JosieAilsa

    Also in the same position of having to conside that same dirty fix for 0 values! 🙏

  7. self-assigned this
    on Jul 5, 2024
  8. removed their assignment
    on Aug 2, 2024
  9. emilykl commented on Aug 28, 2026

    @emilykl
    Contributor

    Codepen from duplicate issue #6544: https://codepen.io/bleah1/pen/dyqLmvN

  10. emilykl commented on Sep 1, 2026

    @emilykl
    Contributor

    Equivalent plotly.py issue: plotly/plotly.py#4686

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3backlogbugsomething broken

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions