I am working in a Jupyter Notebook Python 3 (ipykernel) and trying to create a Sankey plot using plotly via importing a json file with the data. When I run my code, I receive no error, however, there is no plot just an empty output.
I am working from the "More complex Sankey diagram with colored links" example shown here: https://plotly.com/python/sankey-diagram/#reference
- I checked the json to make sure that there is no circularity (i.e., that the same two nodes are not being used as the source and target in two separate entries: e.g., entry1: source=A, target=B; entry2: source=B, target=A).
- Another post I found mentioned that the target number has to be greater than the source number https://community.plotly.com/t/cannot-plot-sankey-diagram/9734, so I check this as well at that is not the issue.
- I found another post here https://community.plotly.com/t/sankey-diagram-appearing-as-blank/4959/7 that said something about adding prefixes to the labels, but I was not able to find out what this means.
Any thoughts? Thank you!
Here is the code:
import plotly.graph_objs as go
import json
with open("sankeywhst.json", 'r') as j:
data = json.loads(j.read())
fig = go.Figure(data=[go.Sankey(
valueformat = ".0f",
valuesuffix = "TWh",
# Define nodes
node = dict(
pad = 15,
thickness = 15,
line = dict(color = "black", width = 0.5),
label = data['data'][0]['node']['label'],
color = data['data'][0]['node']['color']
),
# Add links
link = dict(
source = data['data'][0]['link']['source'],
target = data['data'][0]['link']['target'],
value = data['data'][0]['link']['value'],
label = data['data'][0]['link']['label'],
color = data['data'][0]['link']['color']
))])
fig.update_layout(title_text="Flow of WHST Files",
font_size=10)
fig.show()
Here is the json:
{"data": [
{
"type": "sankey",
"domain": {
"x": [
0,
1
],
"y": [
0,
1
]
},
"orientation": "h",
"valueformat": ".0f",
"valuesuffix": "TWh",
"node": {
"pad": 15,
"thickness": 15,
"line": {
"color": "black",
"width": 0.5
},
"label": [
"All potentially generated",
"Generated",
"Not generated",
"Ingested within 48 hours",
"Ingested after 48 hours",
"Processed within 48 hours",
"Processed after 48 hours (already late)",
"Visualized within 48 hours",
"Visualized after 48 hours (otherwise on time)",
"Visualized after 48 hours (already late)",
"Excluded Fitbit Sleep files",
"Not intended for visualization: Fitbit Steps files",
"Not intended for visualization: Fitbit Steps files (already late)",
"Not intended for visualization: Fitbit Steps files (never generated)",
"Never visualized (otherwise on time)",
"Never visualized (already late)",
"Never visualized (never generated)"
],
"color": [
"rgba(120, 0, 189, 0.8)",
"rgba(61, 0, 255, 0.8)",
"rgba(217, 0, 0, 0.8)",
"rgba(45, 100, 216, 0.8)",
"rgba(255, 128, 54, 0.8)",
"rgba(0, 210, 143, 0.8)",
"rgba(210, 94, 0, 0.8)",
"rgba(18, 197, 0, 0.8)",
"rgba(255, 210, 0, 0.8)",
"rgba(210, 94, 0, 0.8)",
"rgba(195, 0, 197, 0.8)",
"rgba(158, 0, 197, 0.8)",
"rgba(210, 94, 0, 0.8)",
"rgba(217, 0, 0, 0.8)",
"rgba(255, 210, 0, 0.8)",
"rgba(210, 94, 0, 0.8)",
"rgba(217, 0, 0, 0.8)"
]
},
"link": {
"source": [
0,
0,
1,
1,
2,
2,
3,
4,
5,
5,
5,
5,
5,
6,
6,
6
],
"target": [
1,
2,
3,
4,
13,
16,
5,
6,
10,
7,
8,
11,
14,
9,
12,
15
],
"value": [
924,
156,
891,
33,
24,
132,
891,
33,
9,
279,
410,
193,
0,
24,
8,
1
],
"color": [
"rgba(120, 0, 189, 0.2)",
"rgba(120, 0, 189, 0.2)",
"rgba(61, 0, 255, 0.2)",
"rgba(61, 0, 255, 0.2)",
"rgba(217, 0, 0, 0.2)",
"rgba(217, 0, 0, 0.2)",
"rgba(45, 100, 216, 0.2)",
"rgba(255, 128, 54, 0.2)",
"rgba(0, 210, 143, 0.2)",
"rgba(0, 210, 143, 0.2)",
"rgba(0, 210, 143, 0.2)",
"rgba(0, 210, 143, 0.2)",
"rgba(0, 210, 143, 0.2)",
"rgba(210, 94, 0, 0.2)",
"rgba(210, 94, 0, 0.2)",
"rgba(210, 94, 0, 0.2)"
],
"label": [
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
}
}],
"layout": {
"title": {"text": "WHST Combined Data Flow"},
"width": 1118,
"height": 772,
"font": {
"size": 10
},
"updatemenus": [
{
"y": 1,
"buttons": [
{
"label": "Light",
"method": "relayout",
"args": [ "paper_bgcolor", "white" ]
},
{
"label": "Dark",
"method": "relayout",
"args": [ "paper_bgcolor", "black"]
}
]
},
{
"y": 0.9,
"buttons": [
{
"label": "Thick",
"method": "restyle",
"args": [ "node.thickness", 15 ]
},
{
"label": "Thin",
"method": "restyle",
"args": [ "node.thickness", 8]
}
]
},
{
"y": 0.8,
"buttons": [
{
"label": "Small gap",
"method": "restyle",
"args": [ "node.pad", 15 ]
},
{
"label": "Large gap",
"method": "restyle",
"args": [ "node.pad", 20]
}
]
},
{
"y": 0.7,
"buttons": [
{
"label": "Snap",
"method": "restyle",
"args": [ "arrangement", "snap" ]
},
{
"label": "Perpendicular",
"method": "restyle",
"args": [ "arrangement", "perpendicular"]
},
{
"label": "Freeform",
"method": "restyle",
"args": [ "arrangement", "freeform"]
},
{
"label": "Fixed",
"method": "restyle",
"args": [ "arrangement", "fixed"]
}
]
},
{
"y": 0.6,
"buttons": [
{
"label": "Horizontal",
"method": "restyle",
"args": [ "orientation", "h" ]
},
{
"label": "Vertical",
"method": "restyle",
"args": [ "orientation", "v"]
}
]
}
]
}
}
Here are two sites that explain how to output plotly graphs in Jupyter Notebooks: The first by using additional libraries Plotly documentation.
The second by changing or explicit calling the renderer Plotly renderers. Hopefully, one of these options works for you.
EDIT I tested the code and the JSON. It all works after I change the last line into
fig.show(renderer="browser"). What has worked for me in the past is taking the following steps when checking if a Plotly figure is successfully created. First, check if there is anything infig.data. This should contain a tuple with at least one item and that is a Sankey diagram. If that is the case and knowing that the figure is created, the second step is finding a renderer that works in your setup by going through them one after an other. After a while you'll know which renderers work for you.