Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何沿x轴移动图形?_Python_Matplotlib - Fatal编程技术网

Python 如何沿x轴移动图形?

Python 如何沿x轴移动图形?,python,matplotlib,Python,Matplotlib,我需要绘制时间序列数据。以下是示例代码: ax2 = pyplot.subplot(212) true_targets = pyplot.plot(test_y[:, 0, :]) guessed_targets = pyplot.plot(test_y_hat[:, 0, :], linestyle='--') 它将生成以下图表: 我想将图形沿x轴向右移动(从1开始,而不是从0开始)。只需使用plot和指定x值的向量即可: plot(x_values, y_values, linestyl

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

ax2 = pyplot.subplot(212)
true_targets = pyplot.plot(test_y[:, 0, :])
guessed_targets = pyplot.plot(test_y_hat[:, 0, :], linestyle='--')
它将生成以下图表:


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

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

plot(x_values, y_values, linestyles etc)
而不仅仅是

plot(y_values, linestyles etc)
当然,
x_值
y_值
需要具有相同的长度。您可以通过执行以下操作轻松确保

plot(range(1,1+len(y_values)), y_values, linestyles etc)
我通常会发现自己处于一种情况,整数不会将其切割为x坐标,因此我经常使用numpy(
import numpy
)并


n_点
通常只是
len(y_值)
也许我误解了这里的问题,但是如果您只是希望x轴从1开始而不是从0开始(不更改任何值),您可以在轴上设置x限制:

ax2.set_xlim(1,len(test_y_hat))
您可以使用一些条件逻辑来设置x轴的上限,而不是像上面那样获取向量的长度

ax2.set_xlim(1,len(test_y_hat))