Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delta/Difference Histogram

Minimum working example:

import pandas as pd
import plotly.graph_objects as go

# Example data
data = {
    "data-a": [10, 15, 10, 20, 25, 30, 15, 10, 20, 25],
    "data-b": [12, 18, 14, 22, 28, 35, 17, 13, 21, 27]
}
df = pd.DataFrame(data)

# Create the figure
fig = go.Figure()
fig.add_trace(go.Histogram(x=df["data-a"], name="Data A"))
fig.add_trace(go.Histogram(x=df["data-b"], name="Data B", opacity=0.5))
fig.update_layout(font_size=15)
fig.update_yaxes(title_text="Count")
fig.show()

The result:

enter image description here

I now want to create a new histogram that is basically depicting the difference in counts between both histograms (same X-axis and bins), so in this case that should be something like:

enter image description here

like image 267
Pietair Avatar asked Oct 23 '25 03:10

Pietair


1 Answers

I think that pd.cut().value_counts() is what you're looking for.

import pandas as pd
import plotly.express as px

# Example data
data = {
    "data-a": [10, 15, 10, 20, 25, 30, 15, 10, 20, 25],
    "data-b": [12, 18, 14, 22, 28, 35, 17, 13, 21, 27]
}
df = pd.DataFrame(data)


# Define bins
bin_range = range(9, 40, 5)

# Bin data
binned_data_a = pd.cut(df["data-a"], bins=bin_range).value_counts()
binned_data_b = pd.cut(df["data-b"], bins=bin_range).value_counts()
diff = binned_data_a - binned_data_b

# Plot
px.bar(
    x = bin_range[:-1], 
    y = diff.values, 
    labels={"x": "Bin start value", "y": "Difference (a - b)"}
)

histogram showing difference of A and B

Thanks to @Echedey Luis for suggesting .value_counts(). Also see docs for .cut() and .value_counts().

like image 180
drchicken Avatar answered Oct 26 '25 15:10

drchicken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!