Hi all!
I am learning Python plotting with Plotly Sankey Diagram and I am trying to plot income statements of stocks.
I am unable to figure out why the node and link sizes have a discrepancy at some of the nodes. For example, Income before tax (value=2B) going into Income Tax (value=417M) and Net Income - Cont. Ops (value=1.58B)
Here is the code:
fig = go.Figure(data=[go.Sankey(
node = dict(
pad = 30,
thickness = 20,
line = dict(color = âwhiteâ, width =0),
label = [f"Total Revenue {fmt_df.at[0,'totalRevenue']}", #0 f"Gross Profit {fmt_df.at[0,âgrossProfitâ]}â, #1
f"Cost of Revenue -{fmt_df.at[0,'costOfRevenue']}", #2 f"SG&A -{fmt_df.at[0,âsellingGeneralAdministrativeâ]}â, #3
f"R&D -{fmt_df.at[0,'researchDevelopment']}", #4 f"Other -{fmt_df.at[0,âotherOperatingExpensesâ]}â, #5
f"Operating Income {fmt_df.at[0,'operatingIncome']}", #6 f"Income Before Tax {fmt_df.at[0,âincomeBeforeTaxâ]}â, #7
f"Other Income Expenses {fmt_df.at[0,'totalOtherIncomeExpenseNet']}", #8 f"Income Tax {fmt_df.at[0,âincomeTaxExpenseâ]}â, #9
f"Net Income - Cont Ops {fmt_df.at[0,'netIncomeFromContinuingOps']}", #10 f"Discontinued Operations {fmt_df.at[0,âdiscontinuedOperationsâ]}â, #11
f"Net Income ${fmt_df.at[0,ânetIncomeâ]}" #12
],
color = [â#519E3Fâ,â#519E3Fâ,â#BC271Bâ,â#BC271Bâ,â#BC271Bâ,â#BC271Bâ,â#519E3Fâ,â#519E3Fâ,â#BC271Bâ,â#BC271Bâ,â#519E3Fâ,â#519E3Fâ,â#519E3Fâ]
),
textfont=dict(family=âsans serifâ,size=20,
color=ârgba(0,0,0,1)â),
link = dict(
source = sources,
target = targets,
value = values,
color = link_colors
))])
fig.show()
sources = [0,0,1,1,1,6,6,7,7,10,11]
targets = [1,2,6,3,4,7,8,10,9,12,12]
values = [raw_df.loc[0,âtotalRevenueâ],raw_df.loc[0,âgrossProfitâ],raw_df.loc[0,âcostOfRevenueâ],raw_df.loc[0,âsellingGeneralAdministrativeâ],
raw_df.loc[0,âresearchDevelopmentâ],raw_df.loc[0,âoperatingIncomeâ],raw_df.loc[0,âincomeBeforeTaxâ],abs(raw_df.loc[0,âtotalOtherIncomeExpenseNetâ]),
raw_df.loc[0,âincomeTaxExpenseâ],raw_df.loc[0,ânetIncomeFromContinuingOpsâ],raw_df.loc[0,ânetIncomeâ],raw_df.loc[0,âdiscontinuedOperationsâ]]
link_colors=[â#A4CC9Eâ,â#D58A87â,â#A4CC9Eâ,â#D58A87â,â#D58A87â,â#A4CC9Eâ,â#D58A87â,â#A4CC9Eâ,â#D58A87â,â#A4CC9Eâ,â#A4CC9Eâ] #Colors Green:â#A4CC9Eâ,Red: â#D58A87â
Json format raw_df table:
[{âindexâ:0,âendDateâ:1688083200,âtotalRevenueâ:15475000000,âcostOfRevenueâ:6974000000,âgrossProfitâ:8501000000,âresearchDevelopmentâ:1687000000,âsellingGeneralAdministrativeâ:4900000000,ânonRecurringâ:null,âotherOperatingExpensesâ:-248000000,âtotalOperatingExpensesâ:13313000000,âoperatingIncomeâ:2162000000,âtotalOtherIncomeExpenseNetâ:-162000000,âebitâ:2162000000,âinterestExpenseâ:-423000000,âincomeBeforeTaxâ:2000000000,âincomeTaxExpenseâ:419000000,âminorityInterestâ:70000000,ânetIncomeFromContinuingOpsâ:1581000000,âdiscontinuedOperationsâ:2000000,âextraordinaryItemsâ:null,âeffectOfAccountingChargesâ:null},{âindexâ:1,âendDateâ:1680220800,âtotalRevenueâ:14251000000,âcostOfRevenueâ:6743000000,âgrossProfitâ:7508000000,âresearchDevelopmentâ:1655000000,âsellingGeneralAdministrativeâ:4858000000,ânonRecurringâ:null,âotherOperatingExpensesâ:-180000000,âtotalOperatingExpensesâ:13076000000,âoperatingIncomeâ:1175000000,âtotalOtherIncomeExpenseNetâ:-117000000,âebitâ:1175000000,âinterestExpenseâ:-367000000,âincomeBeforeTaxâ:1058000000,âincomeTaxExpenseâ:124000000,âminorityInterestâ:68000000,ânetIncomeFromContinuingOpsâ:934000000,âdiscontinuedOperationsâ:-7000000,âextraordinaryItemsâ:null,âeffectOfAccountingChargesâ:null},{âindexâ:2,âendDateâ:1672444800,âtotalRevenueâ:16690000000,âcostOfRevenueâ:7058000000,âgrossProfitâ:9632000000,âresearchDevelopmentâ:1604000000,âsellingGeneralAdministrativeâ:4847000000,ânonRecurringâ:null,âotherOperatingExpensesâ:-232000000,âtotalOperatingExpensesâ:13277000000,âoperatingIncomeâ:3413000000,âtotalOtherIncomeExpenseNetâ:-100000000,âebitâ:3413000000,âinterestExpenseâ:-313000000,âincomeBeforeTaxâ:3313000000,âincomeTaxExpenseâ:444000000,âminorityInterestâ:77000000,ânetIncomeFromContinuingOpsâ:2869000000,âdiscontinuedOperationsâ:-159000000,âextraordinaryItemsâ:null,âeffectOfAccountingChargesâ:null},{âindexâ:3,âendDateâ:1664496000,âtotalRevenueâ:14107000000,âcostOfRevenueâ:6677000000,âgrossProfitâ:7430000000,âresearchDevelopmentâ:1611000000,âsellingGeneralAdministrativeâ:4545000000,ânonRecurringâ:null,âotherOperatingExpensesâ:-120000000,âtotalOperatingExpensesâ:12713000000,âoperatingIncomeâ:1394000000,âtotalOtherIncomeExpenseNetâ:-5895000000,âebitâ:1394000000,âinterestExpenseâ:-295000000,âincomeBeforeTaxâ:-4501000000,âincomeTaxExpenseâ:-1287000000,âminorityInterestâ:71000000,ânetIncomeFromContinuingOpsâ:-3214000000,âdiscontinuedOperationsâ:18000000,âextraordinaryItemsâ:null,âeffectOfAccountingChargesâ:null}]
Image of the plot (rearranged manually):
Any ideas or suggestions?