Skip to content

Commit 0449a92

Browse files
Add custom_data argument to px.histogram
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 #3859
1 parent 3f8a650 commit 0449a92

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## Unreleased
66

7+
### Added
8+
- Add the `custom_data` argument to `px.histogram`, so extra columns can be attached to histogram traces for use in Dash callbacks and selection events, as in the other Plotly Express functions [[#3859](https://github.com/plotly/plotly.py/issues/3859)]
9+
710
### Fixed
811
- Fix `mpl_to_plotly` tick marker mirroring, axis positioning, and trace visibility for twinned and overlaid subplots (such as `twinx` and `twiny`) [[#5310](https://github.com/plotly/plotly.py/pull/5310)], with thanks to @robertoffmoura for the contribution!
912
- Fix `mpl_to_plotly` losing matplotlib axis line colors by exporting the color of each axis's displayed spine to the plotly x and y axis `linecolor` [[#5311](https://github.com/plotly/plotly.py/pull/5311)], with thanks to @robertoffmoura for the contribution!

‎plotly/express/_chart_types.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ def histogram(
462462
facet_col_spacing=None,
463463
hover_name=None,
464464
hover_data=None,
465+
custom_data=None,
465466
animation_frame=None,
466467
animation_group=None,
467468
category_orders=None,

‎tests/test_optional/test_px/test_px.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ def test_custom_data_scatter(backend):
6868
)
6969

7070

71+
def test_custom_data_histogram(backend):
72+
tips = nw.from_native(px.data.tips(return_type=backend))
73+
# No custom data
74+
fig = px.histogram(tips.to_native(), x="total_bill")
75+
assert fig.data[0].customdata is None
76+
# Custom data is carried per row of the input, not per bin
77+
fig = px.histogram(tips.to_native(), x="total_bill", custom_data=["tip", "day"])
78+
assert fig.data[0].customdata.shape == (len(tips), 2)
79+
assert np.all(fig.data[0].customdata[:, 0] == tips.get_column("tip").to_numpy())
80+
assert np.all(fig.data[0].customdata[:, 1] == tips.get_column("day").to_numpy())
81+
# Bins are aggregates, so custom data does not show up in the hover label
82+
assert fig.data[0].hovertemplate == "total_bill=%{x}<br>count=%{y}<extra></extra>"
83+
# Each colored trace carries the custom data of its own rows
84+
fig = px.histogram(
85+
tips.to_native(), x="total_bill", color="sex", custom_data=["tip"]
86+
)
87+
for trace in fig.data:
88+
group = tips.filter(nw.col("sex") == trace.name)
89+
assert trace.customdata.shape == (len(group), 1)
90+
assert np.all(trace.customdata[:, 0] == group.get_column("tip").to_numpy())
91+
92+
7193
def test_labels(backend):
7294
tips = nw.from_native(px.data.tips(return_type=backend))
7395
fig = px.scatter(

0 commit comments

Comments
 (0)