如何解决根据标签值对桑基图进行垂直重新排序
我正在尝试绘制 Sankey diagram 中 3 个集群之间的患者流量。我有一个带有 from-to 值的 pd.DataFrame counts
,见下文。要重现此 DF,here 是应该加载到 pd.DataFrame(这是visualize_cluster_flow_counts 函数的输入)的 counts
dict。
from to value
0 C1_1 C1_2 867
1 C1_1 C2_2 405
2 C1_1 C0_2 2
3 C2_1 C1_2 46
4 C2_1 C2_2 458
... ... ... ...
175 C0_20 C0_21 130
176 C0_20 C2_21 1
177 C2_20 C1_21 12
178 C2_20 C0_21 0
179 C2_20 C2_21 96
DataFrame 中的 from
和 to
值表示集群编号(0、1 或 2)和 x 轴的天数(1 到 21 之间)。如果我用这些值绘制桑基图,结果如下:
代码:
import plotly.graph_objects as go
def visualize_cluster_flow_counts(counts):
all_sources = list(set(counts['from'].values.tolist() + counts['to'].values.tolist()))
froms,tos,vals,labs = [],[],[]
for index,row in counts.iterrows():
froms.append(all_sources.index(row.values[0]))
tos.append(all_sources.index(row.values[1]))
vals.append(row[2])
labs.append(row[3])
fig = go.Figure(data=[go.Sankey(
arrangement='snap',node = dict(
pad = 15,thickness = 5,line = dict(color = "black",width = 0.1),label = all_sources,color = "blue"
),link = dict(
source = froms,target = tos,value = vals,label = labs
))])
fig.update_layout(title_text="Patient flow between clusters over time: 48h (2 days) - 504h (21 days)",font_size=10)
fig.show()
visualize_cluster_flow_counts(counts)
但是,我想对条形进行垂直排序,以便 C0 始终在顶部,C1 始终在中间,C2 >总是在底部(或相反,无关紧要)。我知道我们可以将 node.x
和 node.y
设置为 manually assign the coordinates。因此,我将 x 值设置为天数 *(1/天数范围),增量为 +- 0.045。我根据集群值设置 y 值:0、0.5 或 1。然后我获得了下面的图像。垂直顺序很好,但条形之间的垂直边距明显偏离;它们应该与第一个结果相似。
产生这个的代码是:
import plotly.graph_objects as go
def find_node_coordinates(sources):
x_nodes,y_nodes = [],[]
for s in sources:
# Shift each x with +- 0.045
x = float(s.split("_")[-1]) * (1/21)
x_nodes.append(x)
# Choose either 0,0.5 or 1 for the y-value
cluster_number = s[1]
if cluster_number == "0": y = 1
elif cluster_number == "1": y = 0.5
else: y = 1e-09
y_nodes.append(y)
return x_nodes,y_nodes
def visualize_cluster_flow_counts(counts):
all_sources = list(set(counts['from'].values.tolist() + counts['to'].values.tolist()))
node_x,node_y = find_node_coordinates(all_sources)
froms,color = "blue",x = node_x,y = node_y,),font_size=10)
fig.show()
visualize_cluster_flow_counts(counts)
问题:如何固定条形的边距,使结果看起来像第一个结果?因此,为了清楚起见:应该将条形推到底部。或者有没有另一种方式让桑基图可以根据标签值自动对条形进行垂直重新排序?
解决方法
首先,我认为当前公开的 API 没有办法顺利实现您的目标,您可以查看源代码 here。
尝试按如下方式更改您的 find_node_coordinates
函数(请注意,您应该将计数 DataFrame
传递给):
counts = pd.DataFrame(counts_dict)
def find_node_coordinates(sources,counts):
x_nodes,y_nodes = [],[]
flat_on_top = False
range = 1 # The y range
total_margin_width = 0.15
y_range = 1 - total_margin_width
margin = total_margin_width / 2 # From number of Cs
srcs = counts['from'].values.tolist()
dsts = counts['to'].values.tolist()
values = counts['value'].values.tolist()
max_acc = 0
def _calc_day_flux(d=1):
_max_acc = 0
for i in [0,1,2]:
# The first ones
from_source = 'C{}_{}'.format(i,d)
indices = [i for i,val in enumerate(srcs) if val == from_source]
for j in indices:
_max_acc += values[j]
return _max_acc
def _calc_node_io_flux(node_str):
c,d = int(node_str.split('_')[0][-1]),int(node_str.split('_')[1])
_flux_src = 0
_flux_dst = 0
indices_src = [i for i,val in enumerate(srcs) if val == node_str]
indices_dst = [j for j,val in enumerate(dsts) if val == node_str]
for j in indices_src:
_flux_src += values[j]
for j in indices_dst:
_flux_dst += values[j]
return max(_flux_dst,_flux_src)
max_acc = _calc_day_flux()
graph_unit_per_val = y_range / max_acc
print("Graph Unit per Acc Val",graph_unit_per_val)
for s in sources:
# Shift each x with +- 0.045
d = int(s.split("_")[-1])
x = float(d) * (1/21)
x_nodes.append(x)
print(s,_calc_node_io_flux(s))
# Choose either 0,0.5 or 1 for the y-v alue
cluster_number = s[1]
# Flat on Top
if flat_on_top:
if cluster_number == "0":
y = _calc_node_io_flux('C{}_{}'.format(2,d))*graph_unit_per_val + margin + _calc_node_io_flux('C{}_{}'.format(1,d))*graph_unit_per_val + margin + _calc_node_io_flux('C{}_{}'.format(0,d))*graph_unit_per_val/2
elif cluster_number == "1": y = _calc_node_io_flux('C{}_{}'.format(2,d))*graph_unit_per_val + margin + _calc_node_io_flux('C{}_{}'.format(1,d))*graph_unit_per_val/2
else: y = 1e-09
# Flat On Bottom
else:
if cluster_number == "0": y = 1 - (_calc_node_io_flux('C{}_{}'.format(0,d))*graph_unit_per_val / 2)
elif cluster_number == "1": y = 1 - (_calc_node_io_flux('C{}_{}'.format(0,d)) * graph_unit_per_val /2 )
elif cluster_number == "2": y = 1 - (_calc_node_io_flux('C{}_{}'.format(0,d)) * graph_unit_per_val + margin + _calc_node_io_flux('C{}_{}'.format(2,d)) * graph_unit_per_val /2 )
y_nodes.append(y)
return x_nodes,y_nodes
桑基图应该通过它们相应的归一化值来衡量它们的连接宽度,对吗?这里我也一样,首先计算每个节点的通量,然后通过计算归一化坐标,根据每个节点的通量计算每个节点的中心。
这是您的代码的示例输出和修改后的函数,请注意,我试图尽可能地遵守您的代码,因此它有点未优化(例如,可以将节点的值存储在每个指定的源节点之上以避免其通量重新计算)。
flat_on_bottom
版本中存在一些不一致,我认为这是由 Plotly API 的填充或其他内部来源引起的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。