Matplotlib Add Space Betwen Lines and left and right axes
<p>Given the following line graph:</p>

<pre><code>import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MaxNLocator
fig = plt.figure()
ax = fig.add_subplot(111)
xs = range(26)
ys = range(26)
labels = list('abcdefghijklmnopqrstuvwxyz')


def format_fn(tick_val, tick_pos):
    if int(tick_val) in xs:
        return labels[int(tick_val)]
    else:
        return ''
ax.xaxis.set_major_formatter(FuncFormatter(format_fn))
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
ax.plot(xs, ys)
plt.show()
</code></pre>

<p>How can I add space between the left end of the line and the left y-axis and the same for the right side? For example, there should be space between the 0 on the bottom of the y-axis and the 'a' on the left side of the x-axis.</p>

<p>Thanks in advance!</p>

<p>I guess the easiest would be to add a <code>ax.margins(margin)</code> call, where <code>margin</code> is a float between <code>0</code> and <code>1</code> that multiplied by the plot dimensions (width and height) gives the margin size it will be added.</p>

<p>Not sure how it fares with non trivial layouts (subplots, tight layout) but seems to work pretty well with your example.</p>

python matplotlib