Bar chart not showing

Following code draws two traces; a bar chart with a single bar and a line with two points. The x-axis is Date. I can see the line, but I am unable to see the bar chart, even though the legend for the bar chart appears. Is this a bug or am I missing something?

dash version: 2.9.1

import plotly.graph_objects as go
from dash import Dash, dcc, html

fig = go.Figure()

fig.add_trace(
    go.Scatter(
        x=['2024-03-27', '2024-03-28'],  # changing this from date to some other label, works.
        y=[250000, 250000],              # changing this to list of one entry works
        name='Line'
    )
)

fig.add_trace(
    go.Bar(
        x=['2024-03-28'],                # having more than one entry works.
        y=[200000],
        name='bar chart'
    )
)

app = Dash()
app.layout = html.Div([
    dcc.Graph(figure=fig)
])

app.run_server()

Any ideas? This is what I see:

Hi @mojo ,

Make sure the x axis has same data and same length for both traces.

if you don’t have data for certain point, you can set y value as None value , for example at the date β€˜2024-03-27’.

import plotly.graph_objects as go
from dash import Dash, dcc, html

fig = go.Figure()

fig.add_trace(
    go.Scatter(
        x=['2024-03-27', '2024-03-28'],  # changing this from date to some other label, works.
        y=[250000, 250000],              # changing this to list of one entry works
        name='Line'
    )
)

fig.add_trace(
    go.Bar(
        x=['2024-03-27','2024-03-28'],                # having more than one entry works.
        y=[None,200000],
        name='bar chart'
    )
)

app = Dash()
app.layout = html.Div([
    dcc.Graph(figure=fig)
])

app.run_server()

Hope this help.

Hello @farispriadi, thank you for the feedback.

Make sure the x axis has same data and same length for both traces.

That does not appear to be a requirement. If you make the line trace have 3 points (or more) and leave the bar chart trace with two points, it works, as it should.

The failure occurs only when the bar chart trace has a single entry and the line trace has more than one entry, ie a list of length 1.

/m

Hi @mojo dealing with time axes can be tricky.

TL;DR: Increase the bar width. The unit for time axis is in ms.

In this case you do not see the bar, because the width of the bar is too small. If you hide the line trace and resize the graph by clicking the button of the modebar, you’ll see the bar.

import plotly.graph_objects as go
from dash import Dash, dcc, html

fig = go.Figure()

fig.add_trace(
    go.Scatter(
        x=['2024-03-27', '2024-03-28'],  # changing this from date to some other label, works.
        y=[250000, 250000],              # changing this to list of one entry works
        name='Line'
    )
)

fig.add_trace(
    go.Bar(
        x=['2024-03-28'],                # having more than one entry works.
        y=[200000],
        name='bar chart',
        width=3_600_000
    )
)

app = Dash()
app.layout = html.Div([
    dcc.Graph(figure=fig)
])

app.run_server()

2 Likes

@AIMPED well done!

Isnt that interesting! When there is more than one data point the width is 1 day, since its the diff. But a single point defaults to 1ms. Thank you for this.

Would you know if there is a way to say say width = 1Day?

/m

Sure 1 day = 1000ms x 60s x 60m x24h =86_400_000