1

I have thousand of data in X and Y. I am trying to plot an interpolation graph but to start plotting it need to began from negative value.

x = [15000,14000,13000,12000,11000,0,-1000,-10000,-15000]
y = [1,1,1,1,1,0,-1,-1,-1]

How can i make it into this format

x = [-15000,-10000,-1000,0,11000,12000,13000,14000,15000]
y = [-1,-1,-1,0,1,1,1,1,1]
0

2 Answers 2

3

Try this:

x = x[::-1]
y = y[::-1]

I'd call this "reversing" a list, not "swapping" it, but you get the idea.

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you really need to sort the x list, and also move around the y values in the same way the x values were permuted, take a look at this:

>>> x = [15000,14000,13000,12000,11000,0,-1000,-10000,-15000]
>>> y = [1,1,1,1,1,0,-1,-1,-1]
>>> x1, y1 = zip(*sorted(zip(x, y)))
>>> x1
(-15000, -10000, -1000, 0, 11000, 12000, 13000, 14000, 15000)
>>> y1
(-1, -1, -1, 0, 1, 1, 1, 1, 1)

So x1 and y1 are in the orders you want. But they're tuples instead of lists. If you need lists instead, then, e.g.,

x1, y1 = map(list, zip(*sorted(zip(x, y))))

is one way to do it.

Bwt if all you really need is to simply reverse the lists, then @OscarLopez's answer is much easier :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.