Skip to content Skip to sidebar Skip to footer

How To Make X And Y Axes Appear When Using Networkx And Matplotlib?

Hi so I was trying to plot a graph using networkx and matplotlib however, my x and y axes are not showing up despite setting axes to 'on' and also by adding x/y limit to the axis.

Solution 1:

In some previous version of networkx, the ticks and labels were not set off. Now they are - mostly because the numbers on the axes rarely carry any special meaning.

But if they do, you need to turn them on again.

fig, ax = plt.subplots()
nx.draw_networkx_nodes(..., ax=ax)

#...

ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True)

enter image description here

Solution 2:

Quite an old one, but I had problem with the accepted solution. nx.draw_networkx_nodes is not exactly the same as nx.draw (in particular, it doesn't draw the edges by default). But using draw won't display the axis by itself.

Adding plt.limits("on") allows to use draw (and its syntax) with axis.

fig, ax = plt.subplots()
nx.draw(G,...,ax=ax) #notice we call draw, and not draw_networkx_nodes
limits=plt.axis('on') # turns on axis
ax.tick_params(left=True, bottom=True, labelleft=True, labelbottom=True)

Post a Comment for "How To Make X And Y Axes Appear When Using Networkx And Matplotlib?"