As for the font size, according to the source code, you can take advantage of the SankeyNode
class:
This class defines and draws a single node in the sankey diagram. It
shouldn't need to be created
by the end user, but one may want to edit some properties after creation.
In your code, adding the following line of code after you created the s
variable will do the trick, if you want to set the font size of all elements to, let's say, 28, and align all the labels to the right instead of the left:
for node_list in s.nodes:
for node in node_list:
node.label_opts = {"fontsize": 28}
node.label_pos='right'
For changing the font size or the position of a single specific node, you have to identify it by its index or by its name. Examples:
#changing the third node at the fourth horizontal level (i.e. "Research and development")
s.nodes[3][2].label_pos='right'
s.nodes[3][2].label_opts = {"fontsize":18}
#changing the node called "Provision for income taxes"
node = s.find_node("Provision for\nincome taxes")[0]
node.label_pos='right'
node.label_opts = {"fontsize":25}
The list of options for customizing the nodes (color, padding, etc.) is available in the source code, have a look at the beginning of __init__
function of the SankeyNode
class.
Complete example, combining all the examples above and integrating them in your code:
from sankeyflow import Sankey
import matplotlib.pyplot as plt
plt.figure(figsize=(20, 10), dpi=144)
flows = [
('Product', 'Total revenue', 20779),
('Sevice\nand other', 'Total revenue', 30949),
('Total revenue', 'Gross margin', 34768),
('Total revenue', 'Cost of revenue', 16960),
('Gross margin', 'Operating income', 22247),
('Operating income', 'Income before\nincome taxes', 22247, {'flow_color_mode': 'dest'}),
('Other income, net', 'Income before\nincome taxes', 268),
('Gross margin', 'Research and\ndevelopment', 5758),
('Gross margin', 'Sales and marketing', 5379),
('Gross margin', 'General and\nadministrative', 1384),
('Income before\nincome taxes', 'Net income', 18765),
('Income before\nincome taxes', 'Provision for\nincome taxes', 3750),
]
s = Sankey(flows=flows, flow_color_mode='lesser')
#setting a font size of 28 for all node labels
for node_list in s.nodes:
for node in node_list:
node.label_opts["fontsize"] = 28
#changing the third node at the fourth horizontal level (i.e. "Research and development") - aligned to the right, font size of 18
s.nodes[3][2].label_pos='right'
s.nodes[3][2].label_opts = {"fontsize":18}
#changing the node called "Provision for income taxes", aligned to the right, font size of 25
node = s.find_node("Provision for\nincome taxes")[0]
node.label_pos='right'
node.label_opts = {"fontsize":25}
s.draw()
plt.show()