plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

[Feature request] Aggregate hover data for internal nodes in hierarchical plots

Open evangelia96 opened this issue 1 year ago • 0 comments

Hierarchical plots constructed with px.treemap, px.icicle, and px.sunburst, aggregate data at internal nodes as follows:

  • values: sum of children,
  • color: values-weighted sum of children if df[color] is numeric, else "discrete aggregation",
  • hover_data: only supports "discrete aggregation", where for "discrete aggregation", the value of an internal node is mapped to that of its children if all its children have the same value, otherwise the aggregated value is set to "(?)".

Below is an example of a treemap with 2 quantities in hover data, one of which is numeric and could be aggregated at internal nodes, but instead all non-leaf nodes get a "(?)":

df = px.data.gapminder().query("year == 2007")
df["quantity_to_aggregate"] = np.random.random(df.shape[0])

fig = px.treemap(
    df,
    path=[px.Constant("world"), "continent", "country"],
    values="pop",
    color="lifeExp",
    hover_data=["iso_alpha", "quantity_to_aggregate"],
    color_continuous_scale="RdBu",
    color_continuous_midpoint=np.average(df["lifeExp"], weights=df["pop"]),
)
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
fig.show()

Capture

Is it possible to aggregate numeric hover_data in hierarchical plots, perhaps as a sum initially, but potentially also giving the option to apply a different function?

evangelia96 avatar Apr 30 '24 21:04 evangelia96