I took this example code from the documentation and added an extra dimension to the bars and changed them to be stacked:
import plotly.graph_objects as go
from plotly.data import tips
df = tips()
# Summing the values and adding a day order for sorting
summed_values = df.groupby(by="day", as_index=False).sum(numeric_only=True)
day_order_mapping = {"Thur": 0, "Fri": 1, "Sat": 2, "Sun": 3}
summed_values["order"] = summed_values["day"].apply(lambda day: day_order_mapping[day])
summed_values = summed_values.sort_values(by="order")
days_of_week = summed_values["day"].values
total_bills = summed_values["total_bill"].values
number_of_diners = summed_values["size"].values
# Adding some additional data dimension, for example, tips amount
tips_amount = summed_values["tip"].values
# Create a figure object
fig = go.Figure()
# Adding the bar for number of diners
fig.add_trace(
go.Bar(
x=days_of_week,
y=number_of_diners,
name="Total number of diners",
marker=dict(color="paleturquoise"),
)
)
# Adding another bar for tips amount, making the bars stacked
fig.add_trace(
go.Bar(
x=days_of_week,
y=tips_amount,
name="Total tips",
marker=dict(color="lightgreen"),
)
)
# Adding scatter for total bills
fig.add_trace(
go.Scatter(
x=days_of_week,
y=total_bills,
yaxis="y2",
mode="lines+markers",
name="Total bill amount",
marker=dict(color="crimson"),
)
)
# Updating layout to incorporate stacked bars and dual y-axes
fig.update_layout(
barmode='stack',
legend=dict(orientation="h"),
yaxis=dict(
title="Total number of diners and tips",
side="left",
),
yaxis2=dict(
title="Total bill amount",
side="right",
overlaying="y",
tickmode="sync",
),
)
fig.show()
I would like to modify the hover behavior of the graph so that the hover information for all bars in a stack (number of diners and tips) appears unified when hovering over any part of the stack. However, I want the hover behavior for the line chart to remain as it is, displaying information individually for each point when hovered over.
So far, I only found the ability to set the hovermode on a figure basis, so doing fig.update_layout(hovermode="x unified")
would also include the lines.