This is just a sample data created randomly to test about creating sankey diagram.¶
In [2]:
questions = ["Q1","Q2","Q3","Q4","Q5","Q6","Q7"]
responses = ["Strongly Disagree","Disagree","Neutral","Agree","Strongly Agree"]
data = {
"Q1": [0,0,9,37,6],
"Q2": [2,0,10,30,10],
"Q3": [0,0,13,34,5],
"Q4": [0,2,17,31,2],
"Q5": [0,3,10,34,5],
"Q6": [7,19,9,15,2],
"Q7": [2,19,18,13,0]
}
In [10]:
import plotly.graph_objects as go
# --- Labels ---
labels = questions + responses
# --- Build links ---
sources = []
targets = []
values = []
link_colors = []
response_colors = {
"Strongly Disagree": "#B71C1C",
"Disagree": "#F57C00",
"Neutral": "#FBC02D",
"Agree": "#388E3C",
"Strongly Agree": "#1B5E20"
}
for qi, q in enumerate(questions):
for ri, val in enumerate(data[q]):
if val > 0:
sources.append(qi)
targets.append(len(questions) + ri)
values.append(val)
link_colors.append(response_colors[responses[ri]])
# --- Create Sankey figure ONCE ---
fig = go.Figure(go.Sankey(
node=dict(
pad=20,
thickness=20,
label=labels
),
link=dict(
source=sources,
target=targets,
value=values,
color=link_colors
)
))
# --- Layout ---
fig.update_layout(
title_text="Sankey Flow Diagram: Classroom Code-Switching Responses",
font_size=11
)
# --- Show figure ONCE ---
fig.show()
Thought of creating a circular sankey, but could not get the desired output.¶
In [11]:
import plotly.graph_objects as go
# --- Labels ---
labels = questions + responses
# --- Build links ---
sources = []
targets = []
values = []
link_colors = []
response_colors = {
"Strongly Disagree": "#B71C1C",
"Disagree": "#F57C00",
"Neutral": "#FBC02D",
"Agree": "#388E3C",
"Strongly Agree": "#1B5E20"
}
for qi, q in enumerate(questions):
for ri, val in enumerate(data[q]):
if val > 0:
sources.append(qi)
targets.append(len(questions) + ri)
values.append(val)
link_colors.append(response_colors[responses[ri]])
# --- Circular Sankey ---
fig = go.Figure(go.Sankey(
arrangement="freeform", # enables circular layout
node=dict(
pad=15,
thickness=18,
label=labels,
line=dict(color="black", width=0.5)
),
link=dict(
source=sources,
target=targets,
value=values,
color=link_colors
)
))
# --- Layout ---
fig.update_layout(
title_text="Circular Sankey Diagram: Classroom Code-Switching Responses",
font_size=11,
height=700,
width=900
)
# --- Show once ---
fig.show()