【问题标题】:Make plotly treemap also show negative values (Python)使 plotly treemap 也显示负值(Python)
【发布时间】:2023-01-27 19:10:24
【问题描述】:

我在 python 中有以下代码来创建一个树状图,它应该显示资产价值因股价变化而发生的变化。但是,我只能做到,显示资产的积极变化,而消极的都被排除在外。查看结果:

树状图:

基础数据框(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()

我如何更新代码以显示负资产变化?

谢谢你的帮助!

【问题讨论】:

    标签: python plotly display treemap


    【解决方案1】:

    在 plotly 中,与其他工具(如 Power BI 或 Tableau)相同,树图中每个矩形的面积基于值与值之和的比较。

    例如,值[10, 10, 5] 将占用区域的[40%, 40%, 20%]

    负区域不存在,因此如果对您的情况有意义,您需要一个解决方法:

    1. 在数据框中创建一个绝对值为Asset Change 的新列。
      df_week["Abs Asset Change"]=df_week["Asset Change"].abs()
      
      1. 对于值,请改用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")
      

      我用一些虚拟值检查了它并且它起作用了。如果您有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2021-10-23
      • 1970-01-01
      • 2021-08-26
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      相关资源
      最近更新 更多