I have an undirected graph created by Networkx that I am displaying using pyplot and I want to allow the user to specify the spacing between grid lines. I don't want to manually enter the ticks as this requires knowing the final size of the plot (if there's a way to do this I would like to know) which could vary based on the graph being displayed.
Is there any method that allows you to set the spacing amount? I've looked for a while and can't find anything, thanks.
The code below relates to the creating of the plot not the graph.
#Spacing between each line
intervals = float(sys.argv[1])
nx.draw(displayGraph, pos, node_size = 10)
plt.axis('on')
plt.grid('on')
plt.savefig("test1.png")
I need to find a way to get the grid to have spacing intervals that are defined by the user. I've found ways to do it but it relies on also saying how many grid lines you want and that causes the lines to not be evenly spaced over the plot
Not sure if this contravenes your desire not to manually play with ticks, but you can use matplotlib.ticker
to set the ticks to your given interval:
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
fig,ax=plt.subplots()
#Spacing between each line
intervals = float(sys.argv[1])
loc = plticker.MultipleLocator(base=intervals)
ax.xaxis.set_major_locator(loc)
ax.yaxis.set_major_locator(loc)
# Add the grid
ax.grid(which='major', axis='both', linestyle='-')