扩展绘图以提高可读性而不扩展线条

时间:2016-09-01 09:39:17

标签: python matplotlib plot

我正在绘制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 个答案:

答案 0 :(得分:3)

有几种方法可以做到这一点。

在不需要了解现有xlim的情况下,一种简单,自动的方法是使用ax.margins。这将在图的任一侧添加一定数量的数据限制。例如:

ax.margins(x=0.1)

会将当前x范围的10%添加到图的两端。

另一种方法是使用ax.set_xlim明确设置x限制。

答案 1 :(得分:2)

只需更改xlim()即可。类似的东西:

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