简体   繁体   中英

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

just use plot with a vector that specifies the x values:

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. 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_values = numpy.linspace(lower, upper, n_points)

and n_points is usually just 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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