Python 如何绘制networkx图形的节点子集
这是与我的代码类似的代码Python 如何绘制networkx图形的节点子集,python,pandas,matplotlib,plot,networkx,Python,Pandas,Matplotlib,Plot,Networkx,这是与我的代码类似的代码 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&quo
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)
我可以把我喜欢的情节列出来吗?
或者我可以写下我想在它们之间绘制的节点吗?您可以从节点列表中生成一个节点,并按照上面的操作进行绘制:
H = nx.subgraph(G, ["T1","matan","jack","arzu"])
nx.draw(H, with_labels=True, font_weight='bold', node_color='lightblue', node_size=500)
您正在尝试绘制原始图形的子图形。为此,您可以使用以下方法: 现在,只需使用matplotlib中已使用的相同函数简单地绘制新图形
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"])