如何沿x轴移动图形?

Ric*_*ick 8 python matplotlib

我需要绘制时间序列数据.以下是示例代码:

ax2 = pyplot.subplot(212)
true_targets = pyplot.plot(test_y[:, 0, :])
guessed_targets = pyplot.plot(test_y_hat[:, 0, :], linestyle='--')
Run Code Online (Sandbox Code Playgroud)

并生成以下图表:

我现在拥有的情节

我想沿x轴向右移动图形(从1开始而不是0).

Mar*_*ler 11

只需使用plot指定x值的向量:

plot(x_values, y_values, linestyles etc)
Run Code Online (Sandbox Code Playgroud)

而不仅仅是

plot(y_values, linestyles etc)
Run Code Online (Sandbox Code Playgroud)

当然,x_valuesy_values需要具有相同的长度.你可以通过做类似的事情轻松确保

plot(range(1,1+len(y_values)), y_values, linestyles etc)
Run Code Online (Sandbox Code Playgroud)

我经常发现自己处于这样一种情况,即整数不会将其作为x坐标切割,所以我经常使用numpy(import numpy)并且做

x_values = numpy.linspace(lower, upper, n_points)
Run Code Online (Sandbox Code Playgroud)

而且n_points通常只是len(y_values).