matplotlib 如何在这个自我网络中创造箭头?

vsnjm48y  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(92)
import networkx as nx

import matplotlib.pyplot as plt

G = nx.Graph()

G.add_edges_from([('A', 'B'), ('A', 'C'),
                  ('B', 'C'), ('E', 'F'),
                  ('D', 'E'), ('A', 'D'),
                  ('D', 'C'), ('C', 'F'),
                  ('D', 'E')])

ego = 'A'

pos = nx.spring_layout(G)

nx.draw(G, pos, node_color="lavender",
        node_size=800, with_labels=True)

options = {"node_size": 1200, "node_color": "r"}

nx.draw_networkx_nodes(G, pos, nodelist=[ego], **options)

plt.show()

'
我想为自我网络创建箭头。I want it like this.This is my current output
我没有尝试任何东西,因为我不知道如何添加箭头。

ej83mcc0

ej83mcc01#

要显示箭头,您可以定义:arrows=Truearrowstyle

import networkx as nx

import matplotlib.pyplot as plt

G = nx.Graph()

G.add_edges_from([('A', 'B'), ('A', 'C'),('A', 'D'),
                  ('B', 'C'), 
                  ('C', 'D'), ('C', 'F'),
                  ('D', 'E'),
                  ('F', 'E')])

ego = 'A'

pos = nx.spring_layout(G)

nx.draw(G, pos, node_color="lavender",
        arrows=True, arrowstyle='-|>',    #   <-- Add this one
        node_size=800, with_labels=True)

options = {"node_size": 1200, "node_color": "r"}

nx.draw_networkx_nodes(G, pos, nodelist=[ego], **options)

plt.show()
    • 结果**:

相关问题