Radial Tree Layout ——networkx 绘制放射树状图

安装pygraphviz

这部分比较复杂,需要安装graphviz 软件、c/c++编译环境、pygraphviz python包

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

import os
os.environ["PATH"] += os.pathsep + 'E:\\Program Files\\Graphviz\\bin'
import pygraphviz
#********我的库有点问题,一直找不到Graphviz软件安装的位置,所以在代码中配置一下路径

# Create a new graph
G = nx.DiGraph()

# Add nodes and edges
G.add_node("root")
for i in range(1, 6):
    phylum = f"phylum_{i}"
    G.add_node(phylum)
    G.add_edge("root", phylum)
    for j in range((i - 1) * 6 + 1, i * 6 + 1):
        class_name = f"class_{j}"
        G.add_node(class_name)
        G.add_edge(phylum, class_name)

# Use graphviz to compute the positions of each node for a radial layout
pos = graphviz_layout(G, prog='twopi', args='')

# Draw the graph using the computed positions
nx.draw(G, pos, with_labels=True, node_size=500, alpha=0.6, arrows=False)

# Set plot title and show the plot
plt.title("Radial Tree Layout")
plt.show()

结果

Radial Tree Layout ——networkx 绘制放射树状图_第1张图片

你可能感兴趣的:(networkX,python,networkx)