使用networkx绘制图形时显示背景网格(也称为单元格) [英] Show a background grid (a.k.a. cells) when drawing a graph with networkx

查看:128
本文介绍了使用networkx绘制图形时显示背景网格(也称为单元格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用networkx绘制一个网络图,其中节点位于字段上的给定位置.该字段被划分为设定数量的单元,并且节点位置对应于一个单元.绘制图形时,我想在背景中显示一个网格,以便图形中的单元格和距离可见. networkx是否为此提供选项?我找不到一个.我尝试通过pyplot设置网格:

I'm using networkx to draw a graph of a network with nodes at given positions on a field. The field is divided into a set amount of cells and the node positions correspond to a cell. When drawing the graph, I would like to show a grid in the background so the cells and distances in the graph are visible. Does networkx offer an option for that? I could not find one. I tried setting the grid via pyplot:

plt.xlim(0,14)
plt.ylim(0,10)
plt.xticks([x for x in xrange(0,14)])
plt.yticks([x for x in xrange(0,10)])
plt.grid(True)

可单独使用(请参见此处),但也可以在调用时

which works by itself (see here) but when also calling

nx.draw(G, pos)

它显示了没有网格的图形(请参见此处).我猜网格仍然在后台,但是networkx完全在其上绘制.

it shows the graph without the grid (see here). I'm guessing the grid is still in the background but networkx draws completely over it.

那么,有没有办法用plt或我找不到的networkx命令显示网格呢?

So is there any way to show the grid with plt or maybe a networkx command that I didn't find which allows something similar?

edit:这是使用的完整代码.看到(取消)注释nx.draw(G,pos)

edit: Here is the complete code used. See the difference when (un)commenting nx.draw(G,pos)

import numpy as np
import matplotlib.pyplot as plt
import networkx as nx


def quit_figure(event):
    if event.key == 'q':
        plt.close(event.canvas.figure)

nodes = [[1,1],[3,1],[6,1],[3,2],[13,1],[3,5]]
distance_array = [
    [[],[],[1,3],[],[5],[],[],[],[],[],[],[],[],[],[]],
    [[],[3],[0],[2],[],[],[],[],[],[],[],[],[],[],[]],
    [[],[],[],[1,3],[5],[],[],[4],[],[],[],[],[],[]],
    [[],[1],[0],[2,5],[],[],[],[],[],[],[10],[],[],[],[]],
    [[],[],[],[],[],[],[],[2],[],[],[3,5],[],[],[],[]],
    [[],[],[],[3],[0,2],[],[],[],[],[],[4],[],[],[],[],[]],
    [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]],
    [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]],
    [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]]

current_transmission_range = 0
transmission_range = np.zeros((len(nodes)), dtype=int)

pos = {x:[nodes[x][0],nodes[x][1]] for x in xrange(len(nodes))}
graph_nodes = [x for x in xrange(len(nodes))]


current_transmission_range = 0
cid = plt.gcf().canvas.mpl_connect('key_press_event', quit_figure)
plt.xlim(0,14)
plt.ylim(0,10)
plt.xticks([x+0.5 for x in xrange(0,14)], ['        '+str(x) for x in xrange(0,13)])
plt.yticks([x+0.5 for x in xrange(0,10)], ['\n\n'+str(x)+'\n\n\n\n' for x in xrange(0,9)])
plt.grid(True)

G = nx.Graph()
for node in graph_nodes:
    G.add_node(node)
for node in graph_nodes:
    for j in xrange(transmission_range[node]+1):
        for k in distance_array[node][j]:
            if(j <= current_transmission_range):
                G.add_edge(node, k,weight=j)

edge_weight=dict([((u,v,),int(d['weight'])) for u,v,d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G, pos,edge_labels=edge_weight)
# draw graph
# nx.draw(G, pos)
plt.show()

推荐答案

使用plt.grid('on')

Use plt.grid('on')

import networkx as nx
import matplotlib.pyplot as plt

G = nx.path_graph(4)
nx.draw_networkx(G)
plt.grid('on')
plt.show()

如果要使用nx.draw(G)而不是nx.draw_networkx(G),则还必须使用plt.axis('on')打开轴.

If you want to use nx.draw(G) instead of nx.draw_networkx(G) you must also turn on the axis with plt.axis('on').

这篇关于使用networkx绘制图形时显示背景网格(也称为单元格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆