繁体   English   中英

如何 plot 网络x图的节点子集

[英]How to plot a subset of nodes of a networkx graph

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

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)

例如,我喜欢 plot 部分图表,我喜欢 plot 只是["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)

我可以把我喜欢的列表放到 plot 上吗? 或者我可以将我喜欢的节点写入它们之间的 plot 吗?

您可以从节点列表中生成一个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)

在此处输入图像描述

您正在尝试 plot 原始图的子图。 为此,您可以使用networkx.Graph.subgraph方法:

import networkx as nx
import pandas as pd

# Build a graph using a pd.DataFrame
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())

# Build subgraph containing a subset of the nodes, and edges between those nodes
subgraph = G.subgraph(["T1","matan","jack","arzu"])

现在您只需要使用您已经使用过的 matplotlib 中的相同 function 来简单地 plot 您的新图表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM