Getting vertical gridlines to appear in line plot in matplotlib

I want to get both horizontal and vertical grid lines on my plot but only the horizontal grid lines are appearing by default. I am using a pandas.DataFrame from an sql query in python to generate a line plot with dates on the x-axis. I'm not sure why they do not appear on the dates and I have tried to search for an answer to this but couldn't find one.

All I have used to plot the graph is the simple code below.

data.plot()
grid('on')

data is the DataFrame which contains the dates and the data from the sql query.

I have also tried adding the code below but I still get the same output with no vertical grid lines.

ax = plt.axes()        
ax.yaxis.grid() # horizontal lines
ax.xaxis.grid() # vertical lines

Any suggestions?

enter image description here


Solution 1:

You may need to give boolean arg in your calls, e.g. use ax.yaxis.grid(True) instead of ax.yaxis.grid(). Additionally, since you are using both of them you can combine into ax.grid, which works on both, rather than doing it once for each dimension.

ax = plt.gca()
ax.grid(True)

That should sort you out.

Solution 2:

plt.gca().xaxis.grid(True) proved to be the solution for me