Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

plot as I have it now

I want to shift the graph to the right along x-axis (start from 1 instead of 0).

like image 809
Rick Avatar asked Sep 07 '15 08:09

Rick


2 Answers

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

like image 86
Marcus Müller Avatar answered Oct 31 '22 16:10

Marcus Müller


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

like image 45
Simon Avatar answered Oct 31 '22 16:10

Simon