1
votes

I try to add a line to a multicategory bar graph, but it is not shown. Is it a bug or did i missed something?

Offtopic: Can i format the xaxis category (the tick, not the category value)?, because tickformat automatically refers to the subcategory.

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"]
})
fig = go.Figure()
for t in df.type.unique():
    plot_df = df[df.type == t]
    fig.add_trace(go.Bar(
        x=[plot_df.tick, plot_df.category],
        y=abs(plot_df.value),
        name=t,
    ))
total_df = df.groupby(['tick']).sum()
fig.add_trace(
    go.Scatter(
        x=df.tick.unique(),
        y=total_df.value,
        name="Born - Died",
        mode='lines+markers',
    )
)
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"))
1

1 Answers

2
votes

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:

enter image description here

And without it looks like this (notice the y-axis range and the legend):

enter image description here

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:

enter image description here

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()