title: How to force grid to fit data range
body: <p>Is there a way to remove the extra space between the begin (end) of plot and the begin (end) of data range in matplotlib? I want the grids to be limited to the data range, but I cannot remove these extra spaces. See example:</p>
  <pre class="lang-py prettyprint-override"><code>import matplotlib.pyplot as plt
  import numpy as np
  
  x=np.arange(0,4*np.pi,0.1)
  fig, ax = plt.subplots(figsize=(6, 8))
  ax.plot(x,np.sin(x), color=&quot;black&quot;)
  ax.yaxis.grid(True, color =&quot;gray&quot;, linewidth=0.5)
  ax.hlines(y=0.5, color=[66/255, 176/255, 255/255], linestyle='--', xmin=0, xmax=12, linewidth=2)
  ax.spines[&quot;right&quot;].set_visible(False)
  ax.spines[&quot;top&quot;].set_visible(False)
  ax.spines[&quot;bottom&quot;].set_visible(False)
  ax.spines[&quot;left&quot;].set_visible(False)
  plt.show()
  </code></pre>
  <p>I highlight the extra space in red:</p>
  <p><a href="https://i.stack.imgur.com/JHbBk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JHbBk.png" alt="enter image description here" /></a></p>
  <p>I cannot find a way to fit the grid interval in the data or the blue horizontal line ranges. I tried to reduce the <code>xmin</code> argument in the later case but it didn't work. The <a href="https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.axis.YAxis.grid.html" rel="nofollow noreferrer">documentation</a> of <code>ax.yaxis.grid</code> shows only 3 arguments for this method (b, which,**kwargsLine2D), none of them looks to be related to the data range.</p>