Euphe
Euphe

Reputation: 3709

expand plot for readability without expanding lines

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: enter image description here My 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()

Upvotes: 0

Views: 128

Answers (2)

tmdavison
tmdavison

Reputation: 69156

There are a couple of ways to do this.

An easy, automatic way to do this, without needing knowledge of the existing xlim is to use ax.margins. This will add a certain fraction of the data limits to either side of the plot. For example:

ax.margins(x=0.1)

will add 10% of the current x range to both ends of the plot.

Another method is to explicitly set the x limits using ax.set_xlim.

Upvotes: 3

Ohad Eytan
Ohad Eytan

Reputation: 8464

Just change the xlim(). Something like:

xmin, xmax = plt.xlim()   # return the current xlim
plt.xlim(xmax=xmax+1)

Upvotes: 2

Related Questions