Black Lives Matter. Please consider donating to Black Girls Code today.

Continuous color scale not appearing in treemap

Here’s the code and example data.

I’ve tried different data and different color scales.

I can’t figure out what the issue here is.

I’ve multiple locations, and the color scheme is based on when the first location was established in the country. The size of the treemap is created from the count of locations.

I’ve running the code in pycharm


data_treemap['world'] = 'world'											
fig = px.treemap(data_treemap, path=['world','continent','country.name', 'city.state','city.name'], values='count',											
                 color='established', color_continuous_scale='RdBu',hover_data=['1'], maxdepth=3)											
											
											
plotly.offline.plot(fig)	

 world	continent	country.name	city.state	city.name	count	established				
0	world	North America	United States	California	San Francisco	1	1982				
1	world	North America	United States	California	San Francisco	1	1982				
2	world	Europe	Germany	Baden-Württemberg	Mannheim	1	1984				
3	world	Europe	Germany	Bavaria	Munich	1	1984				
4	world	Europe	Poland	Masovian Voivodeship	Warsaw	1	1987				
5	world	Europe	Czech Republic	Hlavní Mesto Praha	Prague	1	1993				
6	world	Europe	Austria	Vienna	Vienna	1	1991				
7	world	Europe	Romania	None	Bucharest	1	1999				
8	world	Asia	Russia	Moscow	Moscow	1	1991				
9	world	Asia	Estonia	Tartumaa	Tartu	1	1999				
10	world	Europe	Finland	Southern Finland	Hämeenlinna	1	1984	```

Are you running the latest version of Plotly? It’s 4.8.2.

Hi, yep running the latest version

Name: plotly
Version: 4.8.2

I ran the code in idle to check if the issue was related to pycharm but the issue persists.

OK, in this case I suspect there are some non-numerical values in your established column, which is causing Plotly Express to read this as a categorical column rather than a continuous one.

Thanks for the reply.

I double-checked the data. Looks ok.

2037 non-null int64

I tried the code with a subset of the data, and I’m still having the same issu

This example works fine though.

df = px.data.gapminder().query(“year == 2007”)
df[“world”] = “world” # in order to have a single root node
fig = px.treemap(df, path=[‘world’, ‘continent’, ‘country’], values=‘pop’,
color=‘lifeExp’, hover_data=[‘iso_alpha’],
color_continuous_scale=‘RdBu’,
color_continuous_midpoint=np.average(df[‘lifeExp’], weights=df[‘pop’]))

plotly.offline.plot(fig)

It must be some other issue with my data. I’ll keep investigating.

I did pd.to_numeric and it’s working now.

Thanks, good to know in future that that’s something to check.

Not sure why I was getting that it was 2037 non-null int64 in my previous post!