Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define the size of a grid on a plot using Matplotlib

Tags:

I am plotting using Matplotlib in Python. I want create plot with grid, and here is an example from a plotting tutorial. In my plot range if the y axis is from 0 to 14 and if I use pylab.grid(True) then it makes a grid with the size of square of two, but I want the size to be 1. How can I force it?

like image 580
ashim Avatar asked May 04 '12 17:05

ashim


People also ask

How do I show grid in Matplotlib?

By default, Matplotlib does not display gridlines on plots. However, you can use the matplotlib. pyplot. grid() function to easily display and customize gridlines on a plot.


1 Answers

Try using ax.grid(True, which='both') to position your grid lines on both major and minor ticks, as suggested here.

EDIT: Or just set your ticks manually, like this:

import matplotlib.pyplot as plt  fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1,2,3,14],'ro-')  # set your ticks manually ax.xaxis.set_ticks([1.,2.,3.,10.]) ax.grid(True)  plt.show() 
like image 64
ev-br Avatar answered Sep 20 '22 13:09

ev-br