I am trying to render a subpot (2 rows 1 column) with a shared datetime x-axis. The top row will contain two line traces displaying the overall volume of tests, while the bottom plot will contain 2 stacked bar traces displaying the number of failed tests of a certain category. Everything works until the selected category only contains failures from one date (trace of length 1), in which case the trace doesnโt render. Printing the figure dictionary shows that the trace is added tot he figure.
Due to the confidentiality of my work, I cannot share the code or images, but I have reproduced the issue by adapting this example from the Plotly documentation.
import plotly.graph_objects as go
import plotly.subplots as splt
from datetime import date
years = [
date(1995, 1, 1), date(1996, 1, 1), date(1997, 1, 1), date(1998, 1, 1),
date(1999, 1, 1), date(2000, 1, 1), date(2001, 1, 1), date(2002, 1, 1),
date(2003, 1, 1), date(2004, 1, 1), date(2005, 1, 1), date(2006, 1, 1),
date(2007, 1, 1), date(2008, 1, 1), date(2009, 1, 1), date(2010, 1, 1),
date(2011, 1, 1), date(2012, 1, 1)
]
rest_of_world = [219, 146, 112, 127, 124, 180, 236, 207, 236, 263,
350, 430, 474, 526, 488, 537, 500, 439]
china = [16, 13, 10, 11, 28, 37, 43, 55, 56, 88, 105, 156, 270,
299, 340, 403, 549, 499]
# fig = go.Figure()
fig = splt.make_subplots(rows=2, cols=1, shared_xaxes=True,
vertical_spacing=0.009)
fig.append_trace(go.Bar(x=years,
y=rest_of_world,
name='Rest of world',
marker_color='rgb(55, 83, 109)'
), row=1, col=1)
fig.append_trace(go.Bar(x=years,
y=china,
name='China',
marker_color='rgb(26, 118, 255)'
), row=1, col=1)
fig.append_trace(go.Bar(x=years[:1],
y=rest_of_world[:1],
name='Rest of world',
marker_color='rgb(55, 83, 109)'
), row=2, col=1)
fig.append_trace(go.Bar(x=years[:1],
y=china[:1],
name='China',
marker_color='rgb(26, 118, 255)'
), row=2, col=1)
fig.update_layout(
title='US Export of Plastic Scrap',
xaxis_tickfont_size=14,
yaxis=dict(
title='USD (millions)',
titlefont_size=16,
tickfont_size=14,
),
legend=dict(
x=0,
y=1.0,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
barmode='group',
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1 # gap between bars of the same location coordinate.
)
fig.show()
Some things that Iโve found:
- Scatter trace of length 1 will render correctly, Bar trace of length 1 will not.
- Bar trace of length greater than 1 will render correctly
- Bar trace of length 1 will render correctly when shared_xaxes=False
- Changing the location within the subplot doesnโt affect whether the trace renders correctly or not.
I would prefer to keep the DateTime x-axis because I need to accommodate varying lengths of time periods. Let me know if there are any workarounds or if this is a bug that can be fixed.