Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 704
Osmond Bishop Avatar asked Apr 18 '13 04:04

Osmond Bishop


People also ask

How do I show minor gridlines in Matplotlib?

Locate minor locator on the axes. Use grid() method to make minor grid lines. To display the figure, use show() method.


2 Answers

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.

like image 155
wim Avatar answered Sep 23 '22 09:09

wim


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

like image 31
matt Avatar answered Sep 25 '22 09:09

matt