2016-09-01 27 views
0

我繪製了2條線和一個圓點,X軸是日期範圍。點最重要,但它出現在圖的邊界上。我想要進一步「擴大」這個情節,以便點的位置更加明顯。 換句話說,我想擴展X軸而不將新值添加到行的Y值。但是,如果我只是將幾個日期添加到行的X值,我會得到「x和y維度必須相同」的錯誤。我試圖給Y添加一些np.NaN值,這樣尺寸是相等的,但是我得到一個「需要整數」的錯誤。 我的情節: enter image description here 我的代碼:爲了可讀性而擴展繪圖而不擴大線條

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() 

回答

2

只要改變xlim()。例如:

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

有幾種方法可以做到這一點。

一個簡單,自動的方式來做到這一點,而不需要知道現有的xlim是使用ax.margins。這將在圖的任一側添加一定比例的數據限制。例如:

ax.margins(x=0.1) 

將在當前圖的兩端添加10%的當前x範圍。

另一種方法是使用ax.set_xlim明確設置x限制。