Skip to content

Add custom_data argument to px.histogram - #5811

Open
bhaskargurram-ai wants to merge 1 commit into
plotly:mainfrom
bhaskargurram-ai:feat/histogram-custom-data
Open

bhaskargurram-ai wants to merge 1 commit into
plotly:mainfrom
bhaskargurram-ai:feat/histogram-custom-data

Conversation

@bhaskargurram-ai

Copy link
Copy Markdown

Link to issue

Closes #3859

Description of change

Adds the custom_data argument to px.histogram. Every other Plotly Express function that builds a trace from rows of the data frame already accepts it, and as @emilykl noted in #3859, "I don't see any reason why custom_data shouldn't be an argument to px.histogram(), I assume it was just an oversight."

The core already handles custom_data generically (it is how hover_data columns reach customdata for the other functions), so adding the parameter to histogram() is the whole change. The selected columns are stored per input row in the trace's customdata, where Dash callbacks and selection handlers can read them; the hover label is unchanged, since histogram bins are aggregates. The docstring entry comes from _doc.py like the other functions.

Demo

import plotly.express as px

df = px.data.tips()
fig = px.histogram(df, x="total_bill", color="sex", custom_data=["tip", "day"])
fig.data[0].customdata.shape  # (87, 2): one row per input row of the "Female" trace
fig.data[0].hovertemplate     # 'total_bill=%{x}<br>count=%{y}<extra></extra>'

On main the same call raises TypeError: histogram() got an unexpected keyword argument 'custom_data'.

Testing strategy

Added test_custom_data_histogram to tests/test_optional/test_px/test_px.py, parametrized over the pandas, pyarrow and polars backends. It checks that customdata is None by default, that custom_data=["tip", "day"] gives one row per input row with the right values, that the hover template is unaffected, and that with color="sex" each trace carries the custom data of its own rows. The px test module passes with the change (ruff format --check . clean).

Additional information

Marginal traces are not affected: the marginal histogram specs only carry their axis column.

Guidelines

Every other Plotly Express function that builds a trace from rows of
the data frame accepts custom_data, but histogram() never did. The
core already handles the argument generically (it is how hover_data
columns reach customdata for the other functions), so adding the
parameter is enough: the selected columns are stored per input row in
the trace's customdata and are available in the events the figure
emits, which is what Dash callbacks and selection handlers need.

Closes plotly#3859

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add custom_data to px.histogram

1 participant