25

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ashim
  • 24,380
  • 29
  • 72
  • 96

2 Answers2

26

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()
ev-br
  • 24,968
  • 9
  • 65
  • 78
8

If you want to follow along with the example you cited:

>>> import numpy
>>> import pylab
>>> t = numpy.arange(0.0, 1.0+0.1, 0.01)
>>> s = numpy.cos(2*2*numpy.pi*t)
>>> pylab.plot(t,s)
>>> pylab.grid(True)
>>> pylab.xticks([i/10.0 for i in range(0,12)])
>>> pylab.show()
sgallen
  • 2,079
  • 13
  • 10