Derek Snow
Derek Snow

Reputation: 21

Plotly Treemap Returns Blank

please see the fast fully contained colab example https://colab.research.google.com/drive/1lzaw1AmYeJtrcPBxcPCZPyeiACvv4U1C?usp=sharing

input:

%%capture
!pip install --upgrade plotly
import pandas as pd 
final = pd.read_csv("https://raw.githubusercontent.com/firmai/random-assets-two/master/test/file.csv")
final.head()
final.dtypes
import plotly.express as px
import numpy as np

typed = "In-sample"
fig = px.treemap(final, path=["Data","Acronym"], values=typed)

fig.show()

output:

[Blank]

Upvotes: 2

Views: 3160

Answers (2)

Fernando Wittmann
Fernando Wittmann

Reputation: 2547

This issue might also be related to how you handle NaN as treemap will raise an error if a parent has NaN. I had this issue when filling NaN with ''. Was able to solve with df.fillna('NaN')

Upvotes: 0

Rob Raymond
Rob Raymond

Reputation: 31196

You are trying to plot too many items in the treemap.

  • I've effectively reduced it to rows that are in top 50th quantile. Very small items are arguably not relevant to this plot.
  • additionally just filtering to values >= 0 resolves

>=0 rows

fig = px.treemap(
    final.loc[final[typed].ge(0)],
    path=["Data", "Acronym"],
    values=typed,
)

quantile approach

import pandas as pd

final = pd.read_csv(
    "https://raw.githubusercontent.com/firmai/random-assets-two/master/test/file.csv"
)
final.head()
final.dtypes
import plotly.express as px
import numpy as np

typed = "In-sample"
fig = px.treemap(
    final.loc[pd.qcut(final[typed], q=4, labels=[0, 1, 2, 3]).ge(2)],
    path=["Data", "Acronym"],
    values=typed,
)

fig

enter image description here

Upvotes: 1

Related Questions