Show background grid (aka cells) when drawing a graph with networkx

I am using networkx to draw a graph of a network with nodes at given positions on a field. The field is divided into a certain number of cells, and the node positions correspond to the cell. When drawing the graph, I would like to show the grid in the background so that the images and distances on the graph are visible. Does networkx offer an option for this? I couldn't find it. I tried to set 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 called

nx.draw(G, pos)

      

shows a graph without a grid (see here ). I am assuming the grid is still in the background but networkx is consuming it completely.

So, is there a way to show the grid using the plt command, or maybe networkx, which I didn't find, which allows something like this?

edit: Here is the complete code. See the difference when (un) commentingnx.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()

      

+3


source to share


2 answers


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()

      



If you want to use nx.draw(G)

instead nx.draw_networkx(G)

, you must also enable the axis with plt.axis('on')

.

enter image description here

+1


source


NetworkX draw

explicitly disables the axis
.

You can avoid using draw

and call drawing sub-functions instead:

#...
nx.draw_networkx_nodes(G,pos)
nx.draw_networkx_edges(G,pos)
plt.show()

      

Or use draw_networkx

as Arik suggested.



This approach will be recommended if you are looking for long-term control over the drawing (as implementation draw

may change).

Of course, if that doesn't bother you, you can just do plt.axes().set_axis_on()

right after the calldraw

enter image description here

0


source







All Articles