简体   繁体   English

如何沿x轴移动图形?

[英]How to shift a graph along the x-axis?

I need to plot time-series data. 我需要绘制时间序列数据。 Here is the sample code: 以下是示例代码:

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

And it produces the following graph: 并生成以下图表:

我现在拥有的情节

I want to shift the graph to the right along x-axis (start from 1 instead of 0). 我想沿x轴向右移动图形(从1开始而不是0)。

just use plot with a vector that specifies the x values: 只需使用带有指定x值的向量的plot

plot(x_values, y_values, linestyles etc)

instead of just 而不仅仅是

plot(y_values, linestyles etc)

Of course, x_values and y_values need to have the same length. 当然, x_valuesy_values需要具有相同的长度。 You can easily ensure that by doing something like 你可以通过做类似的事情轻松确保

plot(range(1,1+len(y_values)), y_values, linestyles etc)

I generally often find myself in a situation where integers don't cut it as x coordinates, so I often use numpy ( import numpy ) and do 我一般经常发现自己的情况是整数不会将其剪切为x坐标,所以我经常使用numpy( import numpy )并且做

x_values = numpy.linspace(lower, upper, n_points)

and n_points is usually just len(y_values) . n_points通常只是len(y_values)

Maybe I'm misunderstanding the problem here, but if you just want your xaxis to start at 1 instead of 0 (without changing any of your values) you can just set the x limit on your axes: 也许我在这里误解了这个问题,但如果你只是希望你的xaxis从1开始而不是0(不改变你的任何值),你可以在你的轴上设置x限制:

ax2.set_xlim(1,len(test_y_hat))

You can have some conditional logic to set the upper limit on the x axis instead of getting the length of the vector like I did above 你可以有一些条件逻辑来设置x轴的上限,而不是像我上面那样得到矢量的长度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM