Plotly:如何在我的 Sankey 图列上写文本?
Plotly: how to write a text over my Sankey diagram columns?
我构建了一个 Sankey diagram using plotly。我想给列命名,给每个列一个列标题,如下面的红色文本:
如何写这些栏目标题?
您可以使用注释添加文本,使用 0, 1, 2
的 x 值将 xref
设置为 "x"
,并使用 1.05
的 y 值yref
设置为 "paper"
,如纸张坐标。这将确保注释位于图上方。
import plotly.graph_objects as go
fig = go.Figure(data=[go.Sankey(
node = dict(
pad = 15,
thickness = 20,
line = dict(color = "black", width = 0.5),
label = ["A1", "A2", "B1", "B2", "C1", "C2"],
color = "blue"
),
link = dict(
source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A1, B1, ...
target = [2, 3, 3, 4, 4, 5],
value = [8, 4, 2, 8, 4, 2]
))])
layout={
}
for x_coordinate, column_name in enumerate(["column 1","column 2","column 3"]):
fig.add_annotation(
x=x_coordinate,
y=1.05,
xref="x",
yref="paper",
text=column_name,
showarrow=False,
font=dict(
family="Courier New, monospace",
size=16,
color="tomato"
),
align="center",
)
fig.update_layout(
title_text="Basic Sankey Diagram",
xaxis={
'showgrid': False, # thin lines in the background
'zeroline': False, # thick line at x=0
'visible': False, # numbers below
},
yaxis={
'showgrid': False, # thin lines in the background
'zeroline': False, # thick line at x=0
'visible': False, # numbers below
}, plot_bgcolor='rgba(0,0,0,0)', font_size=10)
fig.show()
您似乎更喜欢 parcats 图而不是桑基图。但是如果你真的想用 sankey 来做这件事,对 Derek O 的答案稍作修改就可以避免生成你需要隐藏的轴。只需使用 xref="paper"
并将 x
值分布在 0 到 1 的范围内:
import plotly.graph_objects as go
fig = go.Figure(data=[go.Sankey(
node = dict(
pad = 15,
thickness = 20,
line = dict(color = "black", width = 0.5),
label = ["A1", "A2", "B1", "B2", "C1", "C2"],
color = "blue"
),
link = dict(
source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A1, B1, ...
target = [2, 3, 3, 4, 4, 5],
value = [8, 4, 2, 8, 4, 2]
))])
cols = ["column 1","column 2","column 3"]
for x_coordinate, column_name in enumerate(cols):
fig.add_annotation(
x=x_coordinate / (len(cols) - 1),
y=1.05,
xref="paper",
yref="paper",
text=column_name,
showarrow=False,
font=dict(
family="Courier New, monospace",
size=16,
color="tomato"
),
align="center",
)
fig.show()
我构建了一个 Sankey diagram using plotly。我想给列命名,给每个列一个列标题,如下面的红色文本:
如何写这些栏目标题?
您可以使用注释添加文本,使用 0, 1, 2
的 x 值将 xref
设置为 "x"
,并使用 1.05
的 y 值yref
设置为 "paper"
,如纸张坐标。这将确保注释位于图上方。
import plotly.graph_objects as go
fig = go.Figure(data=[go.Sankey(
node = dict(
pad = 15,
thickness = 20,
line = dict(color = "black", width = 0.5),
label = ["A1", "A2", "B1", "B2", "C1", "C2"],
color = "blue"
),
link = dict(
source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A1, B1, ...
target = [2, 3, 3, 4, 4, 5],
value = [8, 4, 2, 8, 4, 2]
))])
layout={
}
for x_coordinate, column_name in enumerate(["column 1","column 2","column 3"]):
fig.add_annotation(
x=x_coordinate,
y=1.05,
xref="x",
yref="paper",
text=column_name,
showarrow=False,
font=dict(
family="Courier New, monospace",
size=16,
color="tomato"
),
align="center",
)
fig.update_layout(
title_text="Basic Sankey Diagram",
xaxis={
'showgrid': False, # thin lines in the background
'zeroline': False, # thick line at x=0
'visible': False, # numbers below
},
yaxis={
'showgrid': False, # thin lines in the background
'zeroline': False, # thick line at x=0
'visible': False, # numbers below
}, plot_bgcolor='rgba(0,0,0,0)', font_size=10)
fig.show()
您似乎更喜欢 parcats 图而不是桑基图。但是如果你真的想用 sankey 来做这件事,对 Derek O 的答案稍作修改就可以避免生成你需要隐藏的轴。只需使用 xref="paper"
并将 x
值分布在 0 到 1 的范围内:
import plotly.graph_objects as go
fig = go.Figure(data=[go.Sankey(
node = dict(
pad = 15,
thickness = 20,
line = dict(color = "black", width = 0.5),
label = ["A1", "A2", "B1", "B2", "C1", "C2"],
color = "blue"
),
link = dict(
source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A1, B1, ...
target = [2, 3, 3, 4, 4, 5],
value = [8, 4, 2, 8, 4, 2]
))])
cols = ["column 1","column 2","column 3"]
for x_coordinate, column_name in enumerate(cols):
fig.add_annotation(
x=x_coordinate / (len(cols) - 1),
y=1.05,
xref="paper",
yref="paper",
text=column_name,
showarrow=False,
font=dict(
family="Courier New, monospace",
size=16,
color="tomato"
),
align="center",
)
fig.show()