This is either a bug or a mis-specification in your setup. It's clear that your fig.add_traces(go.Scatter))
does something since with it it looks like this:
And without it looks like this (notice the y-axis range and the legend):
The issue seems to be the multicategory x-axis. For each of your traces you've got the following x-values:
([0, 1, 2], ['Born', 'Born', 'Born'])
([0, 1, 2], ['Died', 'Died', 'Died'])
([1, 2], ['Died', 'Born'])
[0 1 2]
So when you're trying to fig.add_traces(go.Scatter(x=df.tick.unique()))
where df.tick.unique()
is array([0, 1, 2], dtype=int64)
, it seems that plotly is rightfully confused on what exactly to display where. So what you can do is to retrieve all these different x-values and try which one suits your needs best using:
xadj = [[*d['x']] for d in fig.data]
And then:
fig.add_trace(
go.Scatter(
#x=df.tick.unique().tolist(),
#x=total_df.index,
x=xadj[1],
y=total_df.value.tolist(),
name="Born - Died",
mode='lines+markers',
),secondary_y=True
)
Which will produce this plot:
I believe that this figure tells the story you'd like to share. But if I understand correctly, you'd rather show the purple line centered over [0, 1, 2]
instead of above the categories ['Born', 'Died']. If you're able to switch the order in which the categories of the x-axis appear, this might just be exactly what you need. Take a closer look at the following complete code sample, and we can talk details when you find the time.
Complete code:
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({
"tick": [0, 0, 1, 1, 1, 2, 2, 2],
"value": [12, -6, 9, -14, -10, 9, -10, 5],
"category": ['Born', 'Died', 'Born', 'Died', 'Died', 'Born', 'Died', 'Born'],
"type": ["Penguin", "Lion", "Penguin", "Lion", "Apes", "Penguin", "Lion", "Apes"]
})
from plotly.subplots import make_subplots
# set figure twith multiple y axes
fig = make_subplots(specs=[[{"secondary_y": True}]])
for t in df.type.unique():
plot_df = df[df.type == t]
fig.add_trace(go.Bar(
x=[plot_df.tick, plot_df.category],
#x=[plot_df.category, plot_df.tick],
y=abs(plot_df.value),
name=t,
))
total_df = df.groupby(['tick']).sum()
# xadj =[]
# for d in fig.data:
# xadj.append([*d['x']])
xadj = [[*d['x']] for d in fig.data]
fig.add_trace(
go.Scatter(
#x=df.tick.unique().tolist(),
#x=total_df.index,
x=xadj[1],
y=total_df.value.tolist(),
name="Born - Died",
mode='lines+markers',
),secondary_y=True
)
fig.update_layout({
'barmode': 'stack',
'xaxis': {
'title_text': "Tick",
'dtick': 'M1',
'tickformat': "%m.%Y",
'tickangle': -90,
},
'yaxis': {
'title_text': "Value",
},
})
#fig.write_html(str("./out2.html"))
fig.show()