plotly / plotly/plotly.py

Bar charts don't group when there's only 1 group

Abierto
#5,295 1 comentario 2 reacciones 1 asignado Ver en GitHub

@camdecoster ya está trabajando en esto.

Desde el 1/8/2025.

bug P1
Lenguaje dominante
Python
Estrellas
18.8k
Forks
2.8k
Merge medio
16 h 26 min
PR fusionados (30 d)
21

Descripción

I found this issue while trying to work around https://github.com/plotly/plotly.py/issues/5294

Plotly version: 6.2.0

Minimal Example
import plotly.graph_objects as go
import pandas as pd
import datetime

data = [
    {"date": datetime.date(2025,7,20), "net_sales": 23.34, "gross_sales": 32.98},
    {"date": datetime.date(2025,7,21), "net_sales": 45.21, "gross_sales": 68.12},
    {"date": datetime.date(2025,7,22), "net_sales": 12.98, "gross_sales": 34.54},
    {"date": datetime.date(2025,7,23), "net_sales": 41.32, "gross_sales": 45.32},
]

df = pd.DataFrame.from_records(data)

def make_figure(df, width=None):
    fig = go.Figure()
    fig.add_trace(go.Bar(
        x=df['date'],
        y=df['gross_sales'],
        xperiod=24*60*60*1000,
        xperiod0=df['date'].min(),
        xperiodalignment="middle",
        width=24*60*60*1000 * 0.4,
        opacity=0.6,
        offsetgroup='gross'  # same behaviour exhibited whether or not this is specified
    ))
    fig.add_trace(go.Bar(
        x=df['date'],
        y=df['net_sales'],
        xperiod=24*60*60*1000,
        xperiod0=df['date'].min(),
        xperiodalignment="middle",
        width=24*60*60*1000 * 0.4,
        opacity=0.6,
        offsetgroup='net'  # same behaviour exhibited whether or not this is specified
    ))
    fig.update_layout(
        barmode='group',
        xaxis={
            'ticklabelmode': 'period',
            'tickmode': 'auto',
            'dtick': 24*60*60*1000,
            'range': (df['date'].min(), df['date'].max() + datetime.timedelta(days=1)),
        })
    fig.show(renderer="browser")

make_figure(df)
make_figure(df[0:1])

When plotting a grouped bar chart of aggregated timeseries data, strange behaviour happens when there's only 1 group in the data. When there are multiple groups of data each bar is displayed as one would expect (first screenshot below), but when there's only a single data point the bars overlap (second screenshot). These bars overlap regardless of whether the offsetgroup or alignmentgroup is specified, and the offset must be manually set in order to get consistent behaviour regardless of the data provided.

Image Image

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.