I have barchart that is both grouped and stacked. Now I want to color the Values that are stacked in a different color. My plot currently looks like this (with a marker of the parts that should be colored differently):
The group corresponds to a category and the stacking is based on delta between two value columns (the delta is stacked on top of the lower values).
I want to color the delta (i.e. the upper part of the stacked bar) in a slightly lighter color (here: light blue for Cat1
and light red for Cat1
).
However, I cannot find an appropriate option for doing so.
My data looks like this:
import pandas as pd
data = [
["Station1", 2.5, 3.0, "Cat1"],
["Station1", 3.7, 4.2, "Cat2"],
["Station2", 1.7, 2.1, "Cat1"],
["Station2", 3.9, 4.0, "Cat2"],
]
df = pd.DataFrame(data, columns=["station", "val1", "val2", "category"])
df["delta"] = df["val2"] - df["val1"]
And here is the plotting function:
import plotly.express as px
px.bar(df, x="station", y=["val1", "delta"], color="category", barmode="group").show()
How can I color the delta differently? The solution can also be in plotly (does not have to be plotly express).
Is there a way to do the workaround of manually computing the delta
column?