0

I'm doing my first work with a Plotly Treemap. I want to show the values used for the colors in the blocks.

Using this answer, I was able to create customdata, and the treemap renders however, the values are not in the same order as the dataframe and it shows the 'Growth' value for a different record in some cases. It is also not off-by-one.

Sample data:

Type,Stock,Holding,Growth
Equities,Sasol,500,118.06
ETFs,SATRIX Top 40,500,12.99

Code:

shares_data = pd.read_csv("stocks.csv", index_col=False)

fig = px.treemap(shares_data,
                 path=['Type', 'Stock'],
                 values='Holding',
                 color='Growth',
                 color_continuous_scale=[(0, "red"), (0.05, "yellow"), (1, "green")],
                 )

fig.data[0].customdata = shares_data.Growth

fig.data[0].texttemplate = "<b>%{label}</b><br>Holding: R%{value}<br>Growth: %{customdata:.2f}%<br>"

fig.show()

Rendered:

Rendered

ou_ryperd
  • 2,037
  • 2
  • 18
  • 23

1 Answers1

0

Having read this, I was able to print the fig's data, which included:

 'marker': {'coloraxis': 'coloraxis',
           'colors': array([ 53.02, 25.17, -23.29, 10.21, 57.59,...

So I could then access the values with:

fig.data[0].customdata = fig.data[0].marker.colors

And now it works.

[Because I really struggled with this, I decided to post the answer instead of deleting the question in case someone else in the future finds ity useful.]

ou_ryperd
  • 2,037
  • 2
  • 18
  • 23