expand plot for readability without expanding lines
<p>I am plotting 2 lines and a dot, X axis is a date range. The dot is most important, but it appears on the boundary of the plot. I want to "expand" the plot further right so that the dot position is more visible.
In other words I want to expand the X axis without adding new values to Y values of lines. However if I just add a few dates to X values of lines I get the "x and y dimensions must be equal" error. I tried to add a few np.NaN values to Y so that dimensions are equal, but then I get an error "integer required".
My plot:
<a href="http://i.stack.imgur.com/BwYp0.png" rel="nofollow"><img src="http://i.stack.imgur.com/BwYp0.png" alt="enter image description here"></a>
My code:</p>

<pre><code>fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
plot_x = train_original.index.values
train_y = train_original.values
ax1.plot(plot_x, train_y, 'grey')
x = np.concatenate([np.array([train_original.index.values[-1]]), test_original.index.values])
y = np.concatenate([np.array([train_original.dropna().values[-1]]), test_original.dropna().values])
ax1.plot(x, y, color='grey')
ax1.plot(list(predicted.index.values), list(predicted.values), 'ro')
ax1.axvline(x=train_end, alpha=0.7, linestyle='--',color='blue')
plt.show()
</code></pre>

<p>There are a couple of ways to do this.</p>

<p>An easy, automatic way to do this, without needing knowledge of the existing <code>xlim</code> is to use <a href="http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.margins" rel="nofollow"><code>ax.margins</code></a>. This will add a certain fraction of the data limits to either side of the plot. For example:</p>

<pre><code>ax.margins(x=0.1)
</code></pre>

<p>will add 10% of the current x range to both ends of the plot.</p>

<p>Another method is to explicitly set the x limits using <a href="http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_xlim" rel="nofollow"><code>ax.set_xlim</code></a>.</p>

python matplotlib plot