简体   繁体   English

Plotly 中的多级树状图

[英]Multi-level treemap in Plotly

I am trying to build a treemap in my dash application.我正在尝试在我的破折号应用程序中构建树状图。 My dataset has a hierarchy that does not have a definite level.我的数据集有一个没有明确级别的层次结构。 The original dataset is as below:原始数据集如下:

在此处输入图片说明

The levels of defined after splitting the code by "."用“.”分割代码后定义的级别。 from the Code column.代码列。 After separating into levels, the dataset becomes as displayed in the picture below:分层次后,数据集如下图所示:

在此处输入图片说明

Now if I try to create a map with this现在,如果我尝试用这个创建地图

fig = px.treemap(df, path=['level_1','level_2','level_3','level_4','level_5','level_6',level_7'], branchvalues="total")
fig.show()

I get an error describing that non-leaves rows are not permitted我收到一个错误,说明不允许使用非叶子行

在此处输入图片说明

But, when I put a placeholder value instead of NaN's, I am able to plot the treemap.但是,当我放置一个占位符值而不是 NaN 时,我可以绘制树状图。

在此处输入图片说明

在此处输入图片说明

At first the treemap seems ok.起初树状图似乎没问题。 But, if we drill down the hierarchy, we see that structure in the treemap is not entirely correct.但是,如果我们深入层次结构,我们会发现树图中的结构并不完全正确。 There should be totals lowest level columns, but what we see is the nested placeholders values 'tmp', as shown in the picture below.应该有总计最低级别的列,但我们看到的是嵌套的占位符值“tmp”,如下图所示。

在此处输入图片说明

Am I doing something wrong in building the dataframe structure?我在构建数据帧结构时做错了什么吗?

I had the same issue!我遇到过同样的问题! It seems like plotly treemap requires rectangular dataframe.似乎情节树图需要矩形数据框。 So, when you have None value in various rows/columns;因此,当您在各个行/列中都有None值时; it makes it non-rectangular.它使它成为非矩形。 Replacing None with df.fillna("tmp", inplace=True) solves that issue.df.fillna("tmp", inplace=True)替换None解决了这个问题。

Reference: plotly.express.treemap参考: plotly.express.treemap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM