gpt4 book ai didi

python - 如何绘制 networkx 图的节点子集

转载 作者:行者123 更新时间:2023-12-04 16:39:48 24 4
gpt4 key购买 nike

这是类似于我的代码的代码

import networkx as nx
from matplotlib import pyplot as plt
%matplotlib notebook
import pandas as pd

data={"A":["T1","T2","tom","adi","matan","tali","pimpunzu","jack","arzu"],
"B":["end","end","T1","T1","T1","T2","T2","matan","matan"]}

df=pd.DataFrame.from_dict(data)

G = nx.from_pandas_edgelist(df,source='A',target='B', edge_attr=None, create_using=nx.DiGraph())
f, ax = plt.subplots(figsize=(10, 10))
nx.draw(G, with_labels=True, font_weight='bold', ax=ax)
例如,我喜欢绘制图形的一部分,我喜欢只绘制 ["T1","matan","jack","arzu"]那是我喜欢得到的
data={"A":["jack","arzu","matan"],
"B":["matan","matan","T1"]}

df=pd.DataFrame.from_dict(data)

G = nx.from_pandas_edgelist(df,source='A',target='B', edge_attr=None, create_using=nx.DiGraph())
f, ax = plt.subplots(figsize=(10, 10))
nx.draw(G, with_labels=True, font_weight='bold', ax=ax)
我可以列出我喜欢绘制的内容吗?
或者我可以写我喜欢在它们之间绘制的节点吗?

最佳答案

您可以生成一个 nx.subgraph 从节点列表中,并按照上面的方式进行绘制:

H = nx.subgraph(G, ["T1","matan","jack","arzu"])
nx.draw(H, with_labels=True, font_weight='bold', node_color='lightblue', node_size=500)
enter image description here

关于python - 如何绘制 networkx 图的节点子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63169294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com