python - Networkx(或 Graphviz)围绕绘图中心顺时针旋转节点标签

标签 python networkx graphviz

我用 Networkx/Graphviz 绘制了一个类似的图:

import networkx as nx
from networkx.drawing.nx_agraph import graphviz_layout

T = nx.balanced_tree(2, 5)
pos = graphviz_layout(T, prog="twopi")
nx.draw_networkx(T, pos, with_labels=True)
plt.show()

这给了我以下情节:

Example Plot 1

我想要的:

我希望所有节点标签都围绕图形中心顺时针旋转。我该怎么做呢?我更喜欢直接使用 Networkx 或 Graphviz 的方式。

对于“顺时针旋转”,我的意思是类似于极坐标图上的标签:

Example Plot 2

我还尝试了以下代码:

T = nx.balanced_tree(2, 5)
pos = graphviz_layout(T, prog="twopi")
nx.draw_networkx(T, pos, with_labels=False)

text = nx.draw_networkx_labels(T, pos=pos)
for _, t in text.items():
    t.set_rotation('clockwise')
    
plt.show()

但对于set_rotation(),仅支持'horizo​​ntal'、'vertical'、numeric value 或 None

我也发现了类似的问题,但没有一个可以帮助我:

不绘制节点标签,这是 2017 年以来的一个安静的老问题,而且对我来说似乎很复杂: NetworkX node labels relative position

答案是在 2019 年使用 graphviz 是不可能的: https://stackoverflow.com/questions/55009159/change-label-orientation-90º-of-a-node-in-graphviz

最佳答案

@sroush 列出的限制的可能解决方法是一起删除节点标签并使用 plt.text 标记您的节点。

见下面的代码:

import networkx as nx
import matplotlib.pyplot as plt

T = nx.balanced_tree(2, 5)
N_nodes=T.number_of_nodes()
fig,ax=plt.subplots(figsize=(10,5))
#Circular layout as an example
pos = nx.circular_layout(T)

#Setting up the text by using node position 
texts=[plt.text(pos[i][0],pos[i][1],str(i),rotation=(i/N_nodes)*360,fontsize=10,horizontalalignment='center',verticalalignment='center') for i in range(N_nodes)]

#Plot result
nx.draw(T, pos)
ax.set_aspect('equal')
plt.show()

输出给出:

enter image description here

编辑:使用graphviz_layout(T, prog="twopi")布局:

import networkx as nx
import matplotlib.pyplot as plt
from networkx.drawing.nx_agraph import graphviz_layout

T = nx.balanced_tree(2, 5)
N_nodes=T.number_of_nodes()
pos = pos = graphviz_layout(T, prog="twopi")
fig,ax=plt.subplots(figsize=(10,5))
texts=[plt.text(pos[i][0],pos[i][1],str(i),rotation=(i/N_nodes)*360,fontsize=10,horizontalalignment='center',verticalalignment='center') for i in range(N_nodes)]
nx.draw(T, pos)
ax.set_aspect('equal')
plt.show()

输出给出:

enter image description here

编辑 2:微调节点布局:

import networkx as nx
import matplotlib.pyplot as plt
from networkx.drawing.nx_agraph import graphviz_layout

T = nx.balanced_tree(2, 5)
N_nodes=T.number_of_nodes()
first_rot_node=31
N_rot=T.number_of_nodes()-first_rot_node+1
pos = pos = graphviz_layout(T, prog="twopi")

fig,ax=plt.subplots(figsize=(10,5))
cmt=0
for i in range(N_nodes):
  if i>=first_rot_node:
    cmt+=1
    if ((cmt-1)/N_rot)*360<90 or ((cmt-1)/N_rot)*360>=270:
      plt.text(pos[i][0],pos[i][1],str(i),rotation=((cmt-1)/N_rot)*360,fontsize=11,horizontalalignment='center',verticalalignment='center') 
    elif ((cmt-1)/N_rot)*360>=90 and ((cmt-1)/N_rot)*360<270:
      plt.text(pos[i][0],pos[i][1],str(i),rotation=((cmt-1)/N_rot)*360+180,fontsize=11,horizontalalignment='center',verticalalignment='center') 
  else:
     plt.text(pos[i][0],pos[i][1],str(i),rotation=0,fontsize=11,horizontalalignment='center',verticalalignment='center') 

nx.draw(T, pos)
ax.set_aspect('equal')
plt.show()

输出给出: enter image description here

关于python - Networkx(或 Graphviz)围绕绘图中心顺时针旋转节点标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70928461/

相关文章:

Python - 如何使用循环缩短这段重复代码?

python - 如何计算 NetworkX 中单个节点的介数中心性?

python-2.7 - 有人可以解释一下这个 'nx.connected_components()' 是做什么的吗?

graph - 如何在graphviz点节点的TABLE标签中左对齐多行文本文字?

javascript - 如何使用 javascript 为 graphviz 制作动画?

diagram - 如何定义 graphviz 点中边缘和节点形状的双线?

python - 通过 brenda-web 界面将参数传递给 python blender 脚本,如何解析参数?

python - 在 python 中使用元组循环提取数据

python - Keras 和 TensorFlow : Saving non-sequential model weights

python - 如何在 DataFrame 的每一行上添加两列的 value_counts?