簡體   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