如何在 Python 中转换 networkx 图?


要在 Python 中转换networkx图,我们可以执行以下步骤 −

  • 使用 Pandas 数据帧创建数据帧。

  • 使用from_pandas_edgelist()方法,通过包含边列表的 Pandas DataFrame 返回一个图。

  • 使用 matplotlib 绘制图 G。我们可以通过增减键“from”和“to”的列表来转换网络。

  • 要显示图形,使用 show()方法。

示例

import pandas as pd
import networkx as nx
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame({'from': ['A', 'B', 'C', 'A'], 'to': ['D', 'A', 'E', 'C']})
G = nx.from_pandas_edgelist(df, 'from', 'to')
nx.draw(G, with_labels=True, node_size=150, alpha=0.5, linewidths=40)
plt.show()

输出

更新于: 12-5-2021

341 次浏览

启动你的职业

通过完成课程获得认证

开始
广告
© . All rights reserved.