I have the following code in python to create a treemap, which should show the changes in asset values from changing stock prices. However, I can only achieve, that the positive asset changes are displayed, the negatives are all left out. See result:
Underlying dataframe (dfWeek):
treemap = px.treemap(dfWeek, path=["Ticker", "Asset Change", "Price Change %"],
values="Asset Change", color="Price Change %", color_continuous_scale='RdBu',
color_continuous_midpoint=0, title="Weekly Changes in Stocks")
treemap.update_layout(margin=dict(t=50, l=25, r=25, b=25))
treemap.show()
How can I update the code to also get the negative asset changes displayed?
Thanks for your help!
In plotly, same as in other tools such as Power BI or Tableau, the area of each rectangle in the treemap is based on the value compared to the sum of values.
For example, values [10, 10, 5]
would occupy [40%, 40%, 20%]
of the area.
A negative area can't exist, so you need a workaround if it makes sense for your case:
Asset Change
.dfWeek["Abs Asset Change"]=dfWeek["Asset Change"].abs()
Abs Asset Change
instead:fig = px.treemap(data_frame=dfWeek,
path=["Ticker", "Asset Change", "Price Change %"],
values="Abs Asset Change",
color="Price Change %",
color_continuous_scale='RdBu',
color_continuous_midpoint=0,
title="Weekly Changes in Stocks")
I checked it with some dummy values and it worked. Let me know if you have any issues.