How to add different values to parent and child variables in a treemap using Plotly

28 Views Asked by At

In this project that I'm working on, I need to display the mean values on the parent-level(Reference area) and the actual values on the child-level(TIME_PERIOD) using plotly treemap. However, I fail to distinguish the parent and child hover data values. It either shows the sum of the child values as the parent hover value or it shows the mean on both the parent and the child variables. Here is my code:

import plotly.express as px

agr_db['Mean_OBS_VALUE'] = agr_db.groupby('Reference area')['OBS_VALUE'].transform('mean')

fig = px.treemap(agr_db, path=['Reference area', 'TIME_PERIOD'],values='OBS_VALUE', title="Agriculture, Forestry and Fishing Employment Breakdown", hover_data={'Mean_OBS_VALUE': True})

fig.update_traces(hovertemplate='labels=%{label}<br>mean_emp=%{customdata[0]}<extra></extra>')

fig.show()

Any help is very much appreciated.

0

There are 0 best solutions below