Networkx:情节质量问题

时间:2018-10-25 20:13:35

标签: python networkx

下面是使用两种方法绘制的两个网络图。第一个图中的直线和圆看起来比第二个图中的更好和更平滑。但我不能真正说出第二个在图像质量上看起来与第一个不一样的原因。

locations = {
    0:(4,4),
    1:(2,0),
    2:(8,0),
    3:(0,1),
    4:(1,1),
    5:(5,2),
    6:(7,2),
    7:(3,3),
    8:(6,3),
}

edges = [
    (0, 8, {'vehicle': '0'}),
    (8, 6, {'vehicle': '0'}),
    (6, 2, {'vehicle': '0'}),
    (2, 5, {'vehicle': '0'}),
    (5, 0, {'vehicle': '0'}),
    (0, 7, {'vehicle': '1'}),
    (7, 1, {'vehicle': '1'}),
    (1, 4, {'vehicle': '1'}),
    (4, 3, {'vehicle': '1'}),
    (3, 0, {'vehicle': '1'}),
]

G=nx.DiGraph()

G.add_edges_from(edges)

plt.figure(figsize=(15,10))
plt.show()

#vehicle 0
temp = [e for e in edges if e[2]['vehicle'] == '0'] #temporary list that filters the path of vehicle 0
nx.draw_networkx_nodes(G, locations, nodelist=[x[0] for x in temp], node_color='b')
nx.draw_networkx_edges(G, locations, edgelist=temp,
                       width=2, edge_color='b', style='dashed')

#vehicle 1
temp = [e for e in edges if e[2]['vehicle'] == '1']
nx.draw_networkx_nodes(G, locations, nodelist=[x[0] for x in temp], node_color='r')
nx.draw_networkx_edges(G, locations, edgelist=temp,
                       width=2, edge_color='r', style='dashed')

#let's color the node 0 in black
nx.draw_networkx_nodes(G, locations, nodelist=[0], node_color='k')

# labels
nx.draw_networkx_labels(G, locations, font_color='w', font_size=12, font_family='sans-serif')

#print out the graph
plt.axis('on')
plt.show()

enter image description here

第二个图形和代码:

import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()

locations = \
    [(4, 4),  # depot
     (2, 0), (8, 0),  # row 0
     (0, 1), (1, 1),
     (5, 2), (7, 2),
     (3, 3), (6, 3),
     (5, 5), (8, 5),
     (1, 6), (2, 6),
     (3, 7), (6, 7),
     (0, 8), (7, 8)]

v0 = [0, 1, 4, 3, 15, 0]
v1 = [0, 14, 16, 10, 2, 0]
vehicles = [v0, v1]
cl = ["r", "b","green","yellow"]

x=0

for v in vehicles:
    n=0
    e=[]
    node=[]
    for i in v:
        G.add_node(i, pos=(locations[i][0], locations[i][1]))
        # a= [locations[i][0], locations[i][1]]
        # print(a)
        if n > 0:
            # print(n)
            # print(v[n])
            # print (v[n-1])
            u= (v[n-1], v[n])
            e.append(u)
            node.append(i)
            print(e)
            print(node)
            G.add_edge(v[n-1], v[n])
            nx.draw(G, nx.get_node_attributes(G, 'pos'), nodelist=node, edgelist=e, with_labels=True, node_color=cl[x], width=2, edge_color=cl[x], \
            style='dashed', font_color='w', font_size=12, font_family='sans-serif')
            # print(x)
        n += 1
    x+=1
#let's color the node 0 in black
nx.draw_networkx_nodes(G, locations, nodelist=[0], node_color='k')
plt.axis('on')
plt.show()

enter image description here

缩小时(此处可能不太清楚),第二个图形中的线和圆不如第一个图形平滑。这个问题的原因是什么?

2 个答案:

答案 0 :(得分:1)

您多次绘制相同的节点和边。在节点循环外部调用draw函数:

for v in vehicles:
    n=0
    e=[]
    node=[]
    for i in v:
        G.add_node(i, pos=(locations[i][0], locations[i][1]))
        # a= [locations[i][0], locations[i][1]]
        # print(a)
        if n > 0:
            # print(n)
            # print(v[n])
            # print (v[n-1])
            u= (v[n-1], v[n])
            e.append(u)
            node.append(i)
            print(e)
            print(node)
            G.add_edge(v[n-1], v[n])
            # print(x)
        n += 1
    nx.draw(G, nx.get_node_attributes(G, 'pos'), nodelist=node, edgelist=e, with_labels=True, node_color=cl[x], width=2, edge_color=cl[x], \
    style='dashed', font_color='w', font_size=12, font_family='sans-serif')
    x+=1

这张图片的结果是

graph

答案 1 :(得分:0)

您的代码未显示在第一个代码片段中如何初始化plt。这些差异往往来自于matplotlib使用的后端。您可能需要查看以下文档:https://matplotlib.org/faq/usage_faq.html#what-is-a-backend