我在 python 中使用以下代码来创建树形图,该树形图应显示股票价格变化导致的 Assets 值(value)变化。然而,我只能实现,显示积极的 Assets 变化,而将消极的因素全部排除。查看结果:
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()
如何更新代码才能显示负 Assets 更改?
感谢您的帮助!
最佳答案
在绘图中,与 Power BI 或 Tableau 等其他工具相同,树状图中每个矩形的面积基于与值之和相比的值。
例如,值[10, 10, 5]
将占据该区域的[40%, 40%, 20%]
。
负数区域不可能存在,因此如果对您的情况有意义,您需要一个解决方法:
- 在数据框中创建一个新列,其绝对值为
Assets 更改
。
dfWeek["Abs Asset Change"]=dfWeek["Asset Change"].abs()
- 对于值,请使用
Abs Asset Change
代替:
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")
我用一些虚拟值检查了它,它起作用了。如果您有任何问题,请告诉我。
关于python - 使绘图树状图也显示负值(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75255489/