You can use ticker to set the tick locations for the grid. The user can specify the input to MultipleLocator which will "Set a tick on every integer that is multiple of base in the view interval." Here's an example:
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
# Two example plots
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
spacing = 0.5 # This can be your user specified spacing.
minorLocator = MultipleLocator(spacing)
ax1.plot(9 * np.random.rand(10))
# Set minor tick locations.
ax1.yaxis.set_minor_locator(minorLocator)
ax1.xaxis.set_minor_locator(minorLocator)
# Set grid to use minor tick locations.
ax1.grid(which = 'minor')
spacing = 1
minorLocator = MultipleLocator(spacing)
ax2.plot(9 * np.random.rand(10))
# Set minor tick locations.
ax2.yaxis.set_minor_locator(minorLocator)
ax2.xaxis.set_minor_locator(minorLocator)
# Set grid to use minor tick locations.
ax2.grid(which = 'minor')
plt.show()
Edit
To use this along with Networkx you can either create the axes using subplot (or some other function) as above and pass that axes to draw like this.
nx.draw(displayGraph, pos, ax=ax1, node_size = 10)
Or you can call nx.draw as you do in your question and use gca to get the current axis afterwards:
nx.draw(displayGraph, pos, node_size = 10)
ax1 = plt.gca()
|