Hi
I have a treemap, and for some reason it doesnt show in the browser. I will paste the whole script:
Can some one tell me or correct this so it works?
(I dont see the error)
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import pandas as pd
import plotly.express as px
import numpy as np
columns_to_select = ['Date', 'Symbol', 'Open', 'Close']
sp500_daily_df = pd.read_csv('sp500_stocks.csv')[columns_to_select]
sum_per_day_df = sp500_daily_df.groupby('Date').sum().reset_index()
sum_per_day_df['Net'] = sum_per_day_df['Close'] - sum_per_day_df['Open']
sum_per_day_df.sort_values('Net').head()
worst_day = '2020-03-20'
worst_day_df = sp500_daily_df[sp500_daily_df['Date'] == worst_day]
worst_day_df.head()
columns_to_select = ['Symbol', 'Shortname', 'Sector', 'Weight']
company_details_df = pd.read_csv('sp500_companies.csv')[columns_to_select]
worst_day_enriched_df = (worst_day_df.set_index('Symbol')
.join(company_details_df.set_index('Symbol'))
.reset_index()
.dropna())
worst_day_enriched_df.head()
worst_day_enriched_df['PcChangeDay'] = ((worst_day_enriched_df['Close']
- worst_day_enriched_df['Open'])
/ worst_day_enriched_df['Open']
* 100
).round(2)
worst_day_enriched_df.head()
worst_day_enriched_df.to_csv('worst_day_sp500.csv', index=False)
worst_day_sp500_df = pd.read_csv('worst_day_sp500.csv')
fig = px.treemap(worst_day_sp500_df,
path=[px.Constant('Stocks'), 'Sector', 'Symbol'],
values='Weight', color='PcChangeDay',
color_continuous_scale=[ 'Red', "#8b0000", 'Green'])
fig.update_layout(margin = dict(t=30, l=10, r=10, b=10))
fig.show()
# this sorting is important otherwise the values from the df
# and the customdata would get messy
worst_day_enriched_df.sort_values(by=['Symbol','Sector'],inplace=True)
fig = px.treemap(worst_day_sp500_df,
path=[px.Constant('Stocks'), 'Sector', 'Symbol'],
values='Weight', color='PcChangeDay',
color_continuous_scale=[ 'Red', "#8b0000", 'Green'])
# you can change the index if you have multiple plots
fig.data[0].customdata = worst_day_sp500_df['PcChangeDay']
# We use <br> here to break the line between the symbol (label)
# and the daily change that we passed to the customdata above
fig.data[0].texttemplate = "%{label}<br>%{customdata}"
fig.update_traces(textposition="middle center",
selector=dict(type='treemap'))
fig.update_layout(margin = dict(t=30, l=10, r=10, b=10))
# This line removes the the color scale from the chart
fig.update(layout_coloraxis_showscale=False)
fig.show()
the data is here can be downloaded from keras, here: keras